Regular expression – PHP appearance

I have a string, for example:

$foo ='Hello __("How are you") I am __("very good thank you")'

I know this is a strange string, but please come with me: P

I need a regular expression to find The content between __ (“Find content here”)
and put it in an array.

That is, the regular expression will find “how are you” and “very good, thank you You”.

try this:

preg_match_all('/(?<=__\(").*?(?="\))/s', $foo, $matches);
print_r($matches);

means:

(?<= # start positive look behind
__\(" # match the characters'__("'
) # end positive look behind
.*? # match any character and repeat it zero or more times, reluctantly
(?= # start positive look ahead
"\) # match the characters'")'
) # end positive look ahead

Edit

And as Greg said: some people are not familiar with looking around and exclude them from the possibility More readable. Then match everything: __(",string and") and wrap the regular expression that matches the string in parentheses. * ?, to capture only these characters. Then you need to pass $matches [ 1] Get your match. Demo:

preg_match_all('/__\("(.*?)"\)/', $foo, $matc hes);
print_r($matches[1]);

I have a string, for example:

< /p>

$foo ='Hello __("How are you") I am __("very good thank you")'

I know this is a strange string, but please Come with me: P

I need a regular expression to find the content between __ ("Find content here")
and put it in an array.

That is, the regular expression will find "how are you" and "very good, thank you".

Try this:

preg_match_all('/(?<=__\(").*?(?="\))/s', $foo, $matches);
print_r($matches);

Meaning:

(?<= # start positive look behind
__\(" # match the characters'__("'
) # end positive look behind
.*? # match any character and repeat it zero or more times, reluctantly
(?= # start positive look ahead
"\) # match the characters'")'
) # end positive look ahead

Edit

And as Greg said: some people are not familiar Looking around, it might be more readable to exclude them. Then match everything: __(",string and") and wrap the regular expression that matches the string in parentheses. *?, to capture only these characters .Then you need to get your match through $matches[1]. Demo:

preg_match_all('/__\("(.*?)"\)/', $foo, $matches);
print_r($matches[1]);

Leave a Comment

Your email address will not be published.