ankme Posted May 20, 2011 Share Posted May 20, 2011 I have a function that searches first for a line in a file and then for a string in that line. If the string is not found, it concatenates the line with the next line and so on until it finds the string. The following script works but it is time consuming. I want to put a "break" somewhere so that if the line is found, the foreach will break. Can someone please help and tell me how to do this? If I put the break before the recursive call, it wont do the call. If I put it after, it wont get to the break. Thanks! function findEndTag($file, $line, $consolidatedLine) { $endTagFound = strstr($consolidatedLine,"/>"); if (!$endTagFound) { $fileContent = file($file); foreach ($fileContent as $i => $lineArray) { if ($lineArray == $line) { $consolidatedLine = $consolidatedLine." ".$fileContent[$i+1]; return (findEndTag($file, $fileContent[$i+1], $consolidatedLine)); } } } else { return $consolidatedLine; } } Quote Link to comment https://forums.phpfreaks.com/topic/236952-break-in-a-recursive-function/ Share on other sites More sharing options...
cyberRobot Posted May 20, 2011 Share Posted May 20, 2011 It seems like the logic behind the function should be modified. Instead of calling the function again to test the data you just read, why not test the data right away? Maybe switch it to: <?php function findEndTag($file, $line, $consolidatedLine) { $fileContent = file($file); foreach ($fileContent as $i => $lineArray) { if ($lineArray == $line) { $consolidatedLine = $consolidatedLine." ".$fileContent[$i+1]; $endTagFound = strstr($consolidatedLine,"/>"); if (!$endTagFound) { return (findEndTag($file, $fileContent[$i+1], $consolidatedLine)); } else { return $consolidatedLine; } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/236952-break-in-a-recursive-function/#findComment-1217970 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.