ok i finally got a good fix for that.
it was easy to find an already made function to add word wrap everywhere *outside html tags (<>) here:
http://php.net/manual/en/function.wordwrap.php
but then i realized that i really didnt want long words to be broken inside of
tags, thats why im making the code tag have scroll bars, i want it to preserve the code in its integral format to that ppl can just copy it and stick it in the middle of their program without having it bug cos the forum ate chunks of it.
so i kept reading the comments on that php site page and saw someone saying he had a function that did cut words only outside of html tags but also could skip the contents of certain predefined tags...
so i thought great thats exactly what i need..
added that bigass function to my code and then when i was testing it my server was getting really slow out of nowhere unresponsive for a few minutes after i loaded a page...
looks like that fantastic function was not so great after all (probably had a few infinite loops in there...)
so of course i couldnt keep that and kill my server and everybodys...
but the first function, the one that wordwrapped everything excet whats in html codes
code:
function wordWrapIgnoreHTML($string, $length = 45, $wrapString = "\n")
{
$wrapped = '';
$word = '';
$html = false;
$string = (string) $string;
for($i=0;$i<strlen($string);$i+=1)
{
$char = $string[$i];
/** HTML Begins */
if($char === '<')
{
if(!empty($word))
{
$wrapped .= $word;
$word = '';
}
$html = true;
$wrapped .= $char;
}
/** HTML ends */
elseif($char === '>')
{
$html = false;
$wrapped .= $char;
}
/** If this is inside HTML -> append to the wrapped string */
elseif($html)
{
$wrapped .= $char;
}
/** Whitespace characted / new line */
elseif($char === ' ' || $char === "\t" || $char === "\n")
{
$wrapped .= $word.$char;
$word = '';
}
/** Check chars */
else
{
$word .= $char;
if(strlen($word) > $length)
{
$wrapped .= $word.$wrapString;
$word = '';
}
}
}
if($word !== ''){
$wrapped .= $word;
}
return $wrapped;
} |
it has been *corrected by someone from the php team according to the little note under the comment that shows the function
quote
[NOTE BY danbrown AT php DOT net: Contains a bug fix provided by (jaw AT condidact DOT dk) on 02-APR-09 to address an issue where "the last word in a non-html-wrapped string will be omitted."]
|
|
so it sounded trustworthy at least that it wont do an infinite loop on my server and kill my entire buziness!!
to make it skip the code that i removed all i had about the code tag (just commented it out for now) in my bbcode function and instead i went to remove all the contents from the quote tag at the beginning of the function. so it makes it skip that nl2br() the rendering of other bbcode tags and well that wordwrap... and put the stuff back in the code tags at the end of the function... a bit like i also do with html and quote tags already...
and thats it, it now works.. long words in the text are cut off at 60 chars and continue on the next line, but code is not touched
and that fix will be available in 1.034, which will be available sometime today.