dsaba Posted September 24, 2007 Share Posted September 24, 2007 I am using this function which uses regex to grab patterns of anything in between two markers The markers I'm using are left '<root>' and right '</root>' is there a better way to do this kind of regex other than my current one, it usually works but for some reason maybe because of the large amount of data 5 MB, it doesn't work <?php function get_insideMarkers($left, $right, $raw, $returnPattern = 'no') { $leftMarker = preg_quote($left, '/'); $rightMarker = preg_quote($right, '/'); $findBulk = preg_match_all('/'.$leftMarker.'(.*?)'.$rightMarker.'/', $raw, $array); $resultArray = $array[1]; if ($returnPattern != 'no') { return $array[0]; } else { return $resultArray; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/70523-solved-regex-not-doing-the-job/ Share on other sites More sharing options...
effigy Posted September 24, 2007 Share Posted September 24, 2007 If the content contains line breaks you need the /s modifier. Quote Link to comment https://forums.phpfreaks.com/topic/70523-solved-regex-not-doing-the-job/#findComment-354251 Share on other sites More sharing options...
chigley Posted September 24, 2007 Share Posted September 24, 2007 Not regex ~ but it does the job <?php function textbetweenarray($s1,$s2,$s){ $myarray=array(); $s1=strtolower($s1); $s2=strtolower($s2); $L1=strlen($s1); $L2=strlen($s2); $scheck=strtolower($s); do{ $pos1 = strpos($scheck,$s1); if($pos1!==false){ $pos2 = strpos(substr($scheck,$pos1+$L1),$s2); if($pos2!==false){ $myarray[]=substr($s,$pos1+$L1,$pos2); $s=substr($s,$pos1+$L1+$pos2+$L2); $scheck=strtolower($s); } } } while (($pos1!==false)and($pos2!==false)); return $myarray; } $contents = file_get_contents("file.txt"); $root = textbetweenarray("<root>", "</root>", $contents); // You now have an array, $root, containing all you need ?> Quote Link to comment https://forums.phpfreaks.com/topic/70523-solved-regex-not-doing-the-job/#findComment-354253 Share on other sites More sharing options...
dsaba Posted September 24, 2007 Author Share Posted September 24, 2007 awesome! thanks! Quote Link to comment https://forums.phpfreaks.com/topic/70523-solved-regex-not-doing-the-job/#findComment-354266 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.