Hey there again.......after taking an extended hiatus from working on my forum.......(one and a half years....heh), I've decided to fix the word-wrapping issue I was having once and for all.
The problem I was having was that I wanted to be able to wrap my users posts at 65th character when they post long URLs, the thread titles at the 25th character, and the subject line in the search at the 15th character.
I have successfully accomplished this by adding additional code into the bbcode.php file. What I did was effectively copied your original code and duplicated twice more in the file, so that there were 3 instances of the code. Then I changed the 'word_wrap_pass' to 'word_wrap_title' and 'word_wrap_search' specified for the title wrapping and the search wrapping.
After that I changed the character each one wraps at so they wrap correctly at the 65th, 25th, and 15th character. Then I changed your addwrap helper function to 'addwraptitle' and 'addwrapsearch', so that no fatal errors occur by calling the same 'addwrap' function twice.
It seems to be working fine, I'm sure the code could be trimmed down more. Here are some links showing the breaks in the posts, thread titles, and search title:
Here it is wrapping in the post at the 65th character:
http://www.philterphilosophy.com/forum/ ... light=#151
Here it is wrapping in the thread titles at the 25th character:
http://www.philterphilosophy.com/forum/ ... m.php?f=15
And if you go to the search and search for 'hegel' a thread will come up breaking at the 15th character.
Here is the complete code changes I made in the file so you can take a look at them:
bbcode.php
- Code: Select all
// Force Word Wrapping (by TerraFrost)
function word_wrap_pass($message)
{
$userdata['user_wordwrap'] = 65;
$tempText = '';
$finalText = '';
$curCount = $tempCount = 0;
$longestAmp = 9;
$inTag = false;
$ampText = '';
$len = strlen($message);
for ($num=0;$num<$len;$num++)
{
$curChar = $message{$num};
if ($curChar == '<')
{
for ($snum=0;$snum<strlen($ampText);$snum++)
{
addWrap($ampText{$snum},$ampText{$snum+1},$userdata['user_wordwrap'],$finalText,$tempText,$curCount,$tempCount);
}
$ampText = '';
$tempText .= '<';
$inTag = true;
}
elseif ($inTag && $curChar == '>')
{
$tempText .= '>';
$inTag = false;
}
elseif ($inTag)
{
$tempText .= $curChar;
}
elseif ($curChar == '&')
{
for ($snum=0;$snum<strlen($ampText);$snum++)
{
addWrap($ampText{$snum},$ampText{$snum+1},$userdata['user_wordwrap'],$finalText,$tempText,$curCount,$tempCount);
}
$ampText = '&';
}
elseif (strlen($ampText) < $longestAmp && $curChar == ';' && function_exists('html_entity_decode') &&
(strlen(html_entity_decode("$ampText;")) == 1 || preg_match('/^&#[0-9]+$/',$ampText)))
{
addWrap($ampText.';',$message{$num+1},$userdata['user_wordwrap'],$finalText,$tempText,$curCount,$tempCount);
$ampText = '';
}
elseif (strlen($ampText) >= $longestAmp || $curChar == ';')
{
for ($snum=0;$snum<strlen($ampText);$snum++)
{
addWrap($ampText{$snum},$ampText{$snum+1},$userdata['user_wordwrap'],$finalText,$tempText,$curCount,$tempCount);
}
addWrap($curChar,$message{$num+1},$userdata['user_wordwrap'],$finalText,$tempText,$curCount,$tempCount);
$ampText = '';
}
elseif (strlen($ampText) != 0 && strlen($ampText) < $longestAmp)
{
$ampText .= $curChar;
}
else
{
addWrap($curChar,$message{$num+1},$userdata['user_wordwrap'],$finalText,$tempText,$curCount,$tempCount);
}
}
return $finalText . $tempText;
}
function addWrap($curChar,$nextChar,$maxChars,&$finalText,&$tempText,&$curCount,&$tempCount) {
$wrapProhibitedChars = "([{!;,\\/:?}])";
if ($curChar == ' ' || $curChar == "\n")
{
$finalText .= $tempText . $curChar;
$tempText = '';
$curCount = 0;
$curChar = '';
}
elseif ($curCount >= $maxChars)
{
$finalText .= $tempText . ' ';
$tempText = '';
$curCount = 1;
}
else
{
$tempText .= $curChar;
$curCount++;
}
// the following code takes care of (unicode) characters prohibiting non-mandatory breaks directly before them.
// $curChar isn't a " " or "\n"
if ($tempText != '' && $curChar != '')
{
$tempCount++;
}
// $curChar is " " or "\n", but $nextChar prohibits wrapping.
elseif ( ($curCount == 1 && strstr($wrapProhibitedChars,$curChar) !== false) ||
($curCount == 0 && $nextChar != '' && $nextChar != ' ' && $nextChar != "\n" && strstr($wrapProhibitedChars,$nextChar) !== false))
{
$tempCount++;
}
// $curChar and $nextChar aren't both either " " or "\n"
elseif (!($curCount == 0 && ($nextChar == ' ' || $nextChar == "\n")))
{
$tempCount = 0;
}
if ($tempCount >= $maxChars && $tempText == '')
{
$finalText .= ' ';
$tempCount = 1;
$curCount = 2;
}
if ($tempText == '' && $curCount > 0)
{
$finalText .= $curChar;
}
}
// Force Word Wrapping (by TerraFrost)
function word_wrap_title($message)
{
$userdata['user_wordwrap'] = 25;
$tempText = '';
$finalText = '';
$curCount = $tempCount = 0;
$longestAmp = 9;
$inTag = false;
$ampText = '';
$len = strlen($message);
for ($num=0;$num<$len;$num++)
{
$curChar = $message{$num};
if ($curChar == '<')
{
for ($snum=0;$snum<strlen($ampText);$snum++)
{
addwraptitle($ampText{$snum},$ampText{$snum+1},$userdata['user_wordwrap'],$finalText,$tempText,$curCount,$tempCount);
}
$ampText = '';
$tempText .= '<';
$inTag = true;
}
elseif ($inTag && $curChar == '>')
{
$tempText .= '>';
$inTag = false;
}
elseif ($inTag)
{
$tempText .= $curChar;
}
elseif ($curChar == '&')
{
for ($snum=0;$snum<strlen($ampText);$snum++)
{
addwraptitle($ampText{$snum},$ampText{$snum+1},$userdata['user_wordwrap'],$finalText,$tempText,$curCount,$tempCount);
}
$ampText = '&';
}
elseif (strlen($ampText) < $longestAmp && $curChar == ';' && function_exists('html_entity_decode') &&
(strlen(html_entity_decode("$ampText;")) == 1 || preg_match('/^&#[0-9]+$/',$ampText)))
{
addwraptitle($ampText.';',$message{$num+1},$userdata['user_wordwrap'],$finalText,$tempText,$curCount,$tempCount);
$ampText = '';
}
elseif (strlen($ampText) >= $longestAmp || $curChar == ';')
{
for ($snum=0;$snum<strlen($ampText);$snum++)
{
addwraptitle($ampText{$snum},$ampText{$snum+1},$userdata['user_wordwrap'],$finalText,$tempText,$curCount,$tempCount);
}
addwraptitle($curChar,$message{$num+1},$userdata['user_wordwrap'],$finalText,$tempText,$curCount,$tempCount);
$ampText = '';
}
elseif (strlen($ampText) != 0 && strlen($ampText) < $longestAmp)
{
$ampText .= $curChar;
}
else
{
addwraptitle($curChar,$message{$num+1},$userdata['user_wordwrap'],$finalText,$tempText,$curCount,$tempCount);
}
}
return $finalText . $tempText;
}
function addwraptitle($curChar,$nextChar,$maxChars,&$finalText,&$tempText,&$curCount,&$tempCount) {
$wrapProhibitedChars = "([{!;,\\/:?}])";
if ($curChar == ' ' || $curChar == "\n")
{
$finalText .= $tempText . $curChar;
$tempText = '';
$curCount = 0;
$curChar = '';
}
elseif ($curCount >= $maxChars)
{
$finalText .= $tempText . ' ';
$tempText = '';
$curCount = 1;
}
else
{
$tempText .= $curChar;
$curCount++;
}
// the following code takes care of (unicode) characters prohibiting non-mandatory breaks directly before them.
// $curChar isn't a " " or "\n"
if ($tempText != '' && $curChar != '')
{
$tempCount++;
}
// $curChar is " " or "\n", but $nextChar prohibits wrapping.
elseif ( ($curCount == 1 && strstr($wrapProhibitedChars,$curChar) !== false) ||
($curCount == 0 && $nextChar != '' && $nextChar != ' ' && $nextChar != "\n" && strstr($wrapProhibitedChars,$nextChar) !== false))
{
$tempCount++;
}
// $curChar and $nextChar aren't both either " " or "\n"
elseif (!($curCount == 0 && ($nextChar == ' ' || $nextChar == "\n")))
{
$tempCount = 0;
}
if ($tempCount >= $maxChars && $tempText == '')
{
$finalText .= ' ';
$tempCount = 1;
$curCount = 2;
}
if ($tempText == '' && $curCount > 0)
{
$finalText .= $curChar;
}
}
// Force Word Wrapping (by TerraFrost)
function word_wrap_search($message)
{
$userdata['user_wordwrap'] = 15;
$tempText = '';
$finalText = '';
$curCount = $tempCount = 0;
$longestAmp = 9;
$inTag = false;
$ampText = '';
$len = strlen($message);
for ($num=0;$num<$len;$num++)
{
$curChar = $message{$num};
if ($curChar == '<')
{
for ($snum=0;$snum<strlen($ampText);$snum++)
{
addwrapsearch($ampText{$snum},$ampText{$snum+1},$userdata['user_wordwrap'],$finalText,$tempText,$curCount,$tempCount);
}
$ampText = '';
$tempText .= '<';
$inTag = true;
}
elseif ($inTag && $curChar == '>')
{
$tempText .= '>';
$inTag = false;
}
elseif ($inTag)
{
$tempText .= $curChar;
}
elseif ($curChar == '&')
{
for ($snum=0;$snum<strlen($ampText);$snum++)
{
addwrapsearch($ampText{$snum},$ampText{$snum+1},$userdata['user_wordwrap'],$finalText,$tempText,$curCount,$tempCount);
}
$ampText = '&';
}
elseif (strlen($ampText) < $longestAmp && $curChar == ';' && function_exists('html_entity_decode') &&
(strlen(html_entity_decode("$ampText;")) == 1 || preg_match('/^&#[0-9]+$/',$ampText)))
{
addwrapsearch($ampText.';',$message{$num+1},$userdata['user_wordwrap'],$finalText,$tempText,$curCount,$tempCount);
$ampText = '';
}
elseif (strlen($ampText) >= $longestAmp || $curChar == ';')
{
for ($snum=0;$snum<strlen($ampText);$snum++)
{
addwrapsearch($ampText{$snum},$ampText{$snum+1},$userdata['user_wordwrap'],$finalText,$tempText,$curCount,$tempCount);
}
addwrapsearch($curChar,$message{$num+1},$userdata['user_wordwrap'],$finalText,$tempText,$curCount,$tempCount);
$ampText = '';
}
elseif (strlen($ampText) != 0 && strlen($ampText) < $longestAmp)
{
$ampText .= $curChar;
}
else
{
addwrapsearch($curChar,$message{$num+1},$userdata['user_wordwrap'],$finalText,$tempText,$curCount,$tempCount);
}
}
return $finalText . $tempText;
}
function addwrapsearch($curChar,$nextChar,$maxChars,&$finalText,&$tempText,&$curCount,&$tempCount) {
$wrapProhibitedChars = "([{!;,\\/:?}])";
if ($curChar == ' ' || $curChar == "\n")
{
$finalText .= $tempText . $curChar;
$tempText = '';
$curCount = 0;
$curChar = '';
}
elseif ($curCount >= $maxChars)
{
$finalText .= $tempText . ' ';
$tempText = '';
$curCount = 1;
}
else
{
$tempText .= $curChar;
$curCount++;
}
// the following code takes care of (unicode) characters prohibiting non-mandatory breaks directly before them.
// $curChar isn't a " " or "\n"
if ($tempText != '' && $curChar != '')
{
$tempCount++;
}
// $curChar is " " or "\n", but $nextChar prohibits wrapping.
elseif ( ($curCount == 1 && strstr($wrapProhibitedChars,$curChar) !== false) ||
($curCount == 0 && $nextChar != '' && $nextChar != ' ' && $nextChar != "\n" && strstr($wrapProhibitedChars,$nextChar) !== false))
{
$tempCount++;
}
// $curChar and $nextChar aren't both either " " or "\n"
elseif (!($curCount == 0 && ($nextChar == ' ' || $nextChar == "\n")))
{
$tempCount = 0;
}
if ($tempCount >= $maxChars && $tempText == '')
{
$finalText .= ' ';
$tempCount = 1;
$curCount = 2;
}
if ($tempText == '' && $curCount > 0)
{
$finalText .= $curChar;
}
}
viewforum.php
- Code: Select all
#
#-----[ FIND ]------------------------------------------
#
'TOPIC_TITLE' => word_wrap_pass($topic_title),
#
#-----[ REPLACE WITH ]----------------------------------
#
'TOPIC_TITLE' => word_wrap_title($topic_title),
search.php
- Code: Select all
#
#-----[ FIND ]------------------------------------------
#
'TOPIC_TITLE' => word_wrap_pass($topic_title),
#
#-----[ REPLACE WITH ]----------------------------------
#
#
'TOPIC_TITLE' => word_wrap_search($topic_title),
So what do you think?