PHP Regex: Match section of string based on part of section
I am trying to extract the age limitations from strings:
Example:
(Class 4) (0-110, 5yo+) 3m1f Good To Firm 18 fences
Return:
5yo+
Other examples would be 2yo, 3yo, 2-3yo, 4yo+, etc.
How do I match whole word if yo exists. I开发者_StackOverflow tried playing with \b
but cannot get it to return the whole 5yo+
if yo is found :(
This worked for me, and should work for all of the examples you have given:
$str = '(Class 4) (0-110, 5yo+) 3m1f Good To Firm 18 fences';
if (preg_match('/[0-9-]+yo\\+?/', $str, $matches)) {
echo "Matched: $matches[0]\n";
}
s/.*(\b[0-9-]+yo\+?\b).*/\1/
is the regex you seem to be looking for
精彩评论