preg_match_all is returning inconsistent results.
I have 3 REGEXs in an array each of which looks for a word where any one vowel may be replaced by an asterisk.
Here the REGEX tries to find the word beep: /((?!.*[\*].*[\*].*)b[e\*][e\*]p)/ixu"
Th first part of the REGEX uses negative lookahead, (?!.*[\*].*[\*].*), to ensure the word cannot have 2 asterisks. The second part, b[e\*][e\*]p), says look for the word and any vowel can be replaced by an asterisk. Thus the overall rule: find the word and any one vowel may be replaced by an asterisk.
Given the string "beep kook rise", my 3 REGEXs should find all 3 words. The REGEXs DO find the 3 words.
Given the string "beep kook r*se", my 3 REGEXs should find all 3 words. The REGEXs DO find the 3 words.
Given the string "beep k*ok r*se", my 3 REGEXs should find all 3 words. The REGEXs find only r*se. Weird!
Given the string "b*ep k*ok r*se", my 3 REGEXs should find all 3 words. Again the REGEXs find only the word r*se. Weirder!
Given the string "r*se kook b*ep", my 3 REGEXs should find all 3 words. The REGEXs find only b*ep and kook. Ultra-weird!
preg_match_all seems to have a problem when more than one word in the string has a vowel replaced with an asterisk.
What's weird is that I am calling preg_match_all 3 separate times to find the 3 words. One call should not affect the other but it seems to.
One other thing, the 'u' REGEX modifier means UN-greedy.
The output form the above code:
I have 3 REGEXs in an array each of which looks for a word where any one vowel may be replaced by an asterisk.
Here the REGEX tries to find the word beep: /((?!.*[\*].*[\*].*)b[e\*][e\*]p)/ixu"
Th first part of the REGEX uses negative lookahead, (?!.*[\*].*[\*].*), to ensure the word cannot have 2 asterisks. The second part, b[e\*][e\*]p), says look for the word and any vowel can be replaced by an asterisk. Thus the overall rule: find the word and any one vowel may be replaced by an asterisk.
Given the string "beep kook rise", my 3 REGEXs should find all 3 words. The REGEXs DO find the 3 words.
Given the string "beep kook r*se", my 3 REGEXs should find all 3 words. The REGEXs DO find the 3 words.
Given the string "beep k*ok r*se", my 3 REGEXs should find all 3 words. The REGEXs find only r*se. Weird!
Given the string "b*ep k*ok r*se", my 3 REGEXs should find all 3 words. Again the REGEXs find only the word r*se. Weirder!
Given the string "r*se kook b*ep", my 3 REGEXs should find all 3 words. The REGEXs find only b*ep and kook. Ultra-weird!
preg_match_all seems to have a problem when more than one word in the string has a vowel replaced with an asterisk.
What's weird is that I am calling preg_match_all 3 separate times to find the 3 words. One call should not affect the other but it seems to.
One other thing, the 'u' REGEX modifier means UN-greedy.
Code:
<?PHP
//
// Find the words beep, kook or rise. Anyone ONE vowel can be replaced by an asterisk and the REGEX should still find the word.
//
echo 'RUN THE REGEX WITH NEXT STRING==========================================';
$str1 = 'beep kook rise';
FindTheWords($str1);
echo 'RUN THE REGEX WITH NEXT STRING==========================================';
$str1 = 'beep kook r*se';
FindTheWords($str1);
echo 'RUN THE REGEX WITH NEXT STRING==========================================';
$str1 = 'beep k*ok r*se';
FindTheWords($str1);
echo 'RUN THE REGEX WITH NEXT STRING==========================================';
$str1 = 'b*ep k*ok r*se';
FindTheWords($str1);
echo 'RUN THE REGEX WITH NEXT STRING==========================================';
$str1 = 'r*se kook b*ep';
FindTheWords($str1);
exit();
/* ========================================================================================================== */
function FindTheWords($str1)
{
echo '<br> INPUT STRING USED=';
print_r($str1);
// Rather than read the following array of REGEXs, it may be easier to just look at the output to understand the REGEXs.
$REGEXs_array = array("/((?!.*[\*].*[\*].*)b[e\*][e\*]p)/ixu", "/((?!.*[\*].*[\*].*)k[o\*][o\*]k)/ixu", "/((?!.*[\*].*[\*].*)r[i\*]s[e\*])/ixu");
$iOffset = 0;
$TempArray = array("");
$array_elements = count($REGEXs_array); // 3 REGEXs in above array
for ($i=0; $i<$array_elements; $i++) //For each REGEX, loop & look for word in passed string
{
echo '<br> REGEX used=';
var_dump($REGEXs_array[$i]);
preg_match_all($REGEXs_array[$i], $str1, $TempArray, PREG_OFFSET_CAPTURE, $iOffset);
echo "<br> MATCH #$i was FOUND=" . $TempArray[0][0][0];
// $TempArray[0][0][0] contains full match
echo '<br> Position in string=' . $TempArray[0][0][1];
// $TempArray[1] is partial matches - don't care about'em
echo '<br>-----------';
}
}
?>
The output form the above code:
Code:
RUN THE REGEX WITH NEXT STRING==========================================
INPUT STRING USED=beep kook rise
REGEX used=string(37) "/((?!.*[\*].*[\*].*)b[e\*][e\*]p)/ixu"
MATCH #0 was FOUND=beep
Position in string=0
-----------
REGEX used=string(37) "/((?!.*[\*].*[\*].*)k[o\*][o\*]k)/ixu"
MATCH #1 was FOUND=kook
Position in string=5
-----------
REGEX used=string(37) "/((?!.*[\*].*[\*].*)r[i\*]s[e\*])/ixu"
MATCH #2 was FOUND=rise
Position in string=10
-----------RUN THE REGEX WITH NEXT STRING==========================================
INPUT STRING USED=beep kook r*se
REGEX used=string(37) "/((?!.*[\*].*[\*].*)b[e\*][e\*]p)/ixu"
MATCH #0 was FOUND=beep
Position in string=0
-----------
REGEX used=string(37) "/((?!.*[\*].*[\*].*)k[o\*][o\*]k)/ixu"
MATCH #1 was FOUND=kook
Position in string=5
-----------
REGEX used=string(37) "/((?!.*[\*].*[\*].*)r[i\*]s[e\*])/ixu"
MATCH #2 was FOUND=r*se
Position in string=10
-----------RUN THE REGEX WITH NEXT STRING==========================================
INPUT STRING USED=beep k*ok r*se
REGEX used=string(37) "/((?!.*[\*].*[\*].*)b[e\*][e\*]p)/ixu"
MATCH #0 was FOUND=
Position in string=
-----------
REGEX used=string(37) "/((?!.*[\*].*[\*].*)k[o\*][o\*]k)/ixu"
MATCH #1 was FOUND=
Position in string=
-----------
REGEX used=string(37) "/((?!.*[\*].*[\*].*)r[i\*]s[e\*])/ixu"
MATCH #2 was FOUND=r*se
Position in string=10
-----------RUN THE REGEX WITH NEXT STRING==========================================
INPUT STRING USED=b*ep k*ok r*se
REGEX used=string(37) "/((?!.*[\*].*[\*].*)b[e\*][e\*]p)/ixu"
MATCH #0 was FOUND=
Position in string=
-----------
REGEX used=string(37) "/((?!.*[\*].*[\*].*)k[o\*][o\*]k)/ixu"
MATCH #1 was FOUND=
Position in string=
-----------
REGEX used=string(37) "/((?!.*[\*].*[\*].*)r[i\*]s[e\*])/ixu"
MATCH #2 was FOUND=r*se
Position in string=10
-----------RUN THE REGEX WITH NEXT STRING==========================================
INPUT STRING USED=r*se kook b*ep
REGEX used=string(37) "/((?!.*[\*].*[\*].*)b[e\*][e\*]p)/ixu"
MATCH #0 was FOUND=b*ep
Position in string=10
-----------
REGEX used=string(37) "/((?!.*[\*].*[\*].*)k[o\*][o\*]k)/ixu"
MATCH #1 was FOUND=kook
Position in string=5
-----------
REGEX used=string(37) "/((?!.*[\*].*[\*].*)r[i\*]s[e\*])/ixu"
MATCH #2 was FOUND=
Position in string=
-----------