`preg_match_all()` yields incorrect/null results

DeeneFeld

New Member
The questionWhy do I get the wrong results? How can I get the desired results?The input file\[code\]vulture (wing)tabulations: one leg; two legs; flyingfather; master; patriarchmat (box)pedistal; blockade; pilaranimal belly (oval)old style: navaljackal's belly; jester slope of hill (arch)key; visible; enlightened\[/code\]The two versions of the code, with issueVersion 1:\[code\]<?php$filename = "fakexample.txt";$file = fopen($filename, "rb");$myFile = fread($file, filesize($filename));//PLEASE NOTE THAT $STARTSAFE IS IN PLACE IN THIS VERSION AND NOT IN THE NEXTfunction get_between($startString, $endString, $myFile, $startSafe, $endSafe){ //CHANGES WILL START HERE if($startSafe = 0){ $startStringSafe = $startString; } elseif($startSafe = 1){ $startStringSafe = preg_quote($startString, '/'); } //AND END HERE if($endSafe = 0){ $endStringSafe = $endString; } elseif($endSafe = 1){ $endStringSafe = preg_quote($endString, '/'); } //non-greedy match any character between start and end strings. //s modifier should make it also match newlines. preg_match_all("/$startStringSafe(.*?)$endStringSafe/m", $myFile, $matches); return $matches;}$list = get_between("^", ")", $myFile, 0, 1);foreach($list[1] as $list){ echo $list."\n";}?>\[/code\]This code returns nothing.Version 2:\[code\]<?php$filename = "fakexample.txt";$file = fopen($filename, "rb");$myFile = fread($file, filesize($filename));//PLEASE NOTE THAT $STARTSAFE IS NOT IN PLACE IN THIS VERSIONfunction get_between($startString, $endString, $myFile, $startSafe, $endSafe){ //CHANGES START HERE $startStringSafe = $startString; //AND END HERE if($endSafe = 0){ $endStringSafe = $endString; } elseif($endSafe = 1){ $endStringSafe = preg_quote($endString, '/'); } //non-greedy match any character between start and end strings. //s modifier should make it also match newlines. preg_match_all("/$startStringSafe(.*?)$endStringSafe/m", $myFile, $matches); return $matches;}$list = get_between("^", ")", $myFile, 0, 1);foreach($list[1] as $list){ echo $list."\n";}?>\[/code\]This returns \[code\]vulture (wing mat (box animal belly (oval jackal's belly; jester slope of hill (arch\[/code\].The desired output\[code\]vulture mat animal belly jackal's belly; jester slope of hill\[/code\]Marked differences\[code\]vulture **(wing** mat **(box** animal belly **(oval** jackal's belly; jester slope of hill **(arch**\[/code\]
 
Top