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!
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"
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.
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]
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.
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);
$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;
$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);
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.