I have an array of string i need to search an string inside the array using regex i开发者_如何学Gos it possible if so please explain..
$a = preg_grep("/search_word/",$array_of_strings);
print_r($a);
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.
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 !!";
}
}
?>
精彩评论