Reputation: 3461
I'm trying to find list of substrings present in another string.
Here is what i'm thinking:
preg_match_all("/foot|ball|football/", "I like football", $results);
Result:
array(
0 => array(
0 => "foot",
1 => "ball"
)
)
As you can see, it matches foot
and ball
but not football
I've tried different flags, php options and ordering of the regex retring with no luck.
I'm unable to get them all to match.
What i figured out that on single character can only be matched once.
It is able to search "bacwards", eg if i change the regex to /ball|foot|football/
moving the ball before foot, both will get matched but football will not.
Also i've tried this with PCRE (php) and Javascript - same results.
Can i get them all to match?
Upvotes: 0
Views: 118
Reputation: 1717
To match all results, you can use this code:
$allMatches = [];
$words = ['foot', 'ball', 'football'];
foreach ($words as $word) {
if (preg_match("/$word/", 'I like football', $result)) {
$allMatches = array_merge($allMatches, $result);
}
}
print_r($allMatches);
This problem due to the existance of foot and ball in football, so you should test every word separately.
Upvotes: 1