How do I search in an array with preg_match?
Example:
<?php
if( preg_match( '/(my\n+string\n+)/i' , array( 'file' , 'my string => name', 'this') , $match) )
{
//Excelent!!
$items[] = $match[1];
} else {
//Ups! not found!
}
?>
This question is related to
php
regex
arrays
preg-match
$items = array();
foreach ($haystacks as $haystack) {
if (preg_match($pattern, $haystack, $matches)
$items[] = $matches[1];
}
Use preg_grep
$array = preg_grep(
'/(my\n+string\n+)/i',
array( 'file' , 'my string => name', 'this')
);
$haystack = array (
'say hello',
'hello stackoverflow',
'hello world',
'foo bar bas'
);
$matches = preg_grep('/hello/i', $haystack);
print_r($matches);
Output
Array
(
[1] => say hello
[2] => hello stackoverflow
[3] => hello world
)
You can use array_walk
to apply your preg_match
function to each element of the array.
Source: Stackoverflow.com