Is it possible to use a regular expression to search inside an array using php

I have a string array, I have to look for the string inside the array using regex, is this possible, if so, please explain.

+3
source share
3 answers
$a = preg_grep("/search_word/",$array_of_strings);
print_r($a);
+9
source

You can use the loop foreachto scroll through all the elements and use preg_matchfor each of them. If it matches, add it to the match array.

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

A very simple example.

+2
source

You can iterate through an array using a foreach loop and finding a key in each element. Example:

<?php

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

foreach($days as $day) {

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

Source: https://habr.com/ru/post/1734830/


All Articles