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!
}
?>
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In this post I’ll provide you with three different methods of doing what you ask for. I actually recommend using the last snippet, since it’s easiest to comprehend as well as being quite neat in code.
##How do I see what elements in an array that matches my regular expression?
There is a function dedicated for just this purpose,
preg_grep. It will take a regular expression as first parameter, and an array as the second.See the below example:
output
###Documentation
##But I just want to get the value of the specified groups. How?
array_reducewithpreg_matchcan solve this issue in clean manner; see the snippet below.output
Documentation
###Using
array_reduceseems tedious, isn’t there another way?Yes, and this one is actually cleaner though it doesn’t involve using any pre-existing
array_*orpreg_*function.Wrap it in a function if you are going to use this method more than once.
Documentation