3

I have an array of string i need to search an string inside the array using regex is it possible if so please explain..

3 Answers 3

9
$a = preg_grep("/search_word/",$array_of_strings);
print_r($a);
Sign up to request clarification or add additional context in comments.

Comments

2

You can use a foreach loop to loop through all the elements and use a preg_match on each of them. If it matches, add it to an array of matches.

foreach($array as $check) {
    if (preg_match("/expression/", $check)) $matches[] = $check;
}

Very simple example.

Comments

2

You can iterate through the array using a foreach loop and search for the key in each element. An example:

<?php

$days = array('Sunday','Monday','Tuesday');
$key = "Sunday";

foreach($days as $day) {

    if(preg_match("/$key/",$day)) {
        echo "Key $key found !!";
    }
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.