开发者

is it possible to use regex to search inside an array using php

开发者 https://www.devze.com 2022-12-21 08:02 出处:网络
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);

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 !!";
    }
}
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消