Perfidus Posted February 14, 2008 Share Posted February 14, 2008 I have developed this function starting from another I found in PHP manual, it should find substrings between all the pairs of given substrings function extractBetweenDelimeters($inputstr,$delimeterLeft,$delimeterRight) { $number=substr_count($inputstr, $delimeterLeft); $adding=0; for ($k = 0; $k < $number; $k++) { $posLeft = stripos($inputstr,$delimeterLeft,$adding)+strlen($delimeterLeft); $posRight = stripos($inputstr,$delimeterRight,$adding); $values=array(); $values[]= substr($inputstr,$posLeft,$posRight-$posLeft); $adding=$adding+$posRight+strlen($delimeterRight); } return $values; } I'm using the function with a string in which there are two coincidences, but I only get the first one... Can't see why it is ignoring the second one!! Link to comment https://forums.phpfreaks.com/topic/91140-problem-searching-in-a-string/ Share on other sites More sharing options...
Barand Posted February 14, 2008 Share Posted February 14, 2008 try <?php function extractBetweenDelimeters($str,$delimeterLeft,$delimeterRight) { $k = strlen($str); $values = array(); $pos = $pos1 = 0; while (1) { $pos = strpos($str, $delimeterLeft, $pos1); if ($pos !== false){ $pos1 = strpos($str, $delimeterRight, $pos); $values[] = substr($str, $pos+1, $pos1-$pos-1); } else break; $pos = $pos1; } return $values; } $str = "Lorem <ipsum> sit dolor <consecutor> amet"; $val = extractBetweenDelimeters($str,'<','>'); echo '<pre>', print_r($val, true), '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/91140-problem-searching-in-a-string/#findComment-467137 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.