500 server error with recursion?!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nak5ive
Forum Newbie
Posts: 3
Joined: Thu Jul 20, 2006 8:20 pm

500 server error with recursion?!

Post by nak5ive »

Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


can anyone perhaps enlighten me as to why my apache server is throwing a 500 level server error with this code.
the error is specifically logged as "Premature end of script headers: php-cgi.exe"

Code: Select all

function replaceTitles($str) {
	$tag_open = "[ttl[";
	$tag_close = "]]";
	$i = strpos($str, $tag_open);
	if ($i >= 0) {
		// Replace the start tag
		$str = trim(substr($str, 0, $i))."<h3>".trim(substr($str, $i+strlen($tag_open)));
		// Replace the end tag
		$i = strpos($str, $tag_close, $i);
		$str = trim(substr($str, 0, $i))."</h3>".trim(substr($str, $i+strlen($tag_close)));

		// ******************************
		// Recursively check for more title tags
		// ******************************
		
		return replaceTitles($str);
		
	} else
		return $str;
}

$text = "[ttl[bigTitle!    ]]Hello this is my text [ttl[WORD!]]. i love my text[when it's in brackets]";
echo replaceTitles($text);
this function should simply change a string formatted as "[ttl[...]]" to "<h3>...</h3>" for all instances in a long string of text (thus the recursive algorithm)

this algorithm works perfect if i take out the recursive call.

can anyone help on this one?

thanks.

Al.


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Without looking at it, and we have seen this happen before, there is most likely a bug in it which is causing it to loop infinitely.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Per the manual regarding the strpos() function..
Warning:

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

Code: Select all

function replaceTitles($str) {
   $tag_open = "[ttl[";
   $tag_close = "]]";
   $i = strpos($str, $tag_open);
   if (($i >= 0) and ($i !== false)) { // <-- problem might have been here..
      // Replace the start tag
      $str = trim(substr($str, 0, $i))."<h3>".trim(substr($str, $i+strlen($tag_open)));
      // Replace the end tag
      $i = strpos($str, $tag_close, $i);
      $str = trim(substr($str, 0, $i))."</h3>".trim(substr($str, $i+strlen($tag_close)));

      // ******************************
      // Recursively check for more title tags
      // ******************************
      
      return replaceTitles($str);
      
   } else
      return $str;
}

$text = "[ttl[bigTitle!    ]]Hello this is my text [ttl[WORD!]]. i love my text[when it's in brackets]";
echo replaceTitles($text);
nak5ive
Forum Newbie
Posts: 3
Joined: Thu Jul 20, 2006 8:20 pm

Post by nak5ive »

brilliant.
problem solved. thanks very much. i didn't realize strpos() returned anything other than false if it doesn't find an occurence in a string.

thanks again.

Al.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

str_replace is a lot easier that all that substr() stuff.. But it won't trim the titles.

Code: Select all

$text = "[ttl[bigTitle!    ]]Hello this is my text [ttl[WORD!]]. i love my text[when it's in brackets]";
	$text = str_replace(array("[ttl[","]]"),array("<h3>","</h3>"),$text);
	echo $text;
If you need to trim them, regexp to the rescue!

Code: Select all

$text = "[ttl[bigTitle!    ]]Hello this is my text [ttl[WORD!]]. i love my text[when it's in brackets]";
	$regex = "/\[ttl\[(.+)[\s]*\]\]/siU";
	$replacement = "<h3>\\1</h3>";
	echo preg_replace($regex, $replacement, $text);
Quicker than recursing through the text.
nak5ive
Forum Newbie
Posts: 3
Joined: Thu Jul 20, 2006 8:20 pm

Post by nak5ive »

well, i'm not completely familiar with the regexp method, but i will look into it. the problem with using string replace is that my text doesn't ONLY contain the tags "[ttl[...]]" but many different tags to be viewed in the same manner. (ie: "[url=http://somedomain.com[...]]") So, replacing each instance of a "]]" with one tag would mess the whole thing up. i have to single out each closing bracket that follows a specific opening tag and handle them appropriately.
otherwise my code would come out like this:

"...<h3>...</h3>...<a href="...">...</h3>..."

that would be no good.

thanks very much for the help though, i'll explore other methods to accomplish this.

Al.

ps. this network is awesome.
Post Reply