PDA

View Full Version : Problems with vB-Code and News Maximum Characters


Stefan
11-14-2004, 05:24 PM
I enabled vB-Code and "News Maximum Characters" (set to 300) and got display errors, because of truncated html-code which were parsed from bbcode. If I turn off vB-Code all look fine.

I hope you understand my crude english. :D

Brian
11-15-2004, 06:57 PM
If you're going to have a limit on your news posts you need to make sure you set it high enough to include the ending tags for your bbcode, especially if you have quote or php tags in your news posts.

Our Sponsors
 

Stefan
11-16-2004, 03:01 PM
Hello Brian

I want a small number of characters, so the news are not too long. I looked into the news.php and realised that you use fetch_trimmed_title () to trim the text. I think that's no good idea because of the truncated or unclosed html tags.

So I wrote my own function.

function fetch_trimmed_html($html, $chars = -1)
{
global $vboptions;

if ($chars == -1)
{
$chars = $vboptions['lastthreadchars'];
}

$strlen = strlen ($html);

if ($chars)
{
$cnt = 0; // count characters
if ($strlen > $chars)
{
$open=0; // is a tag (between < and >)?
$stop=0; // stop adding characters to output-string except html-tags

for ($i=0; $i<$strlen; $i++)
{
if ($html{$i}=='<')
{
$open=1;
$new_html .= $html{$i};
}
else if ($html{$i}=='>')
{
$open=0;
$new_html .= $html{$i};
}
else if ($open)
{
$new_html .= $html{$i};
}
else
{
$cnt++;
if ( $cnt < $chars )
{
$new_html .= $html{$i};
}
else if ( $cnt >= $chars)
{
if (preg_match("#[\s\t\r\n]#", $html{$i}) && !$stop)
{
$stop=1;
$last_i = $i;
}
if (!$stop)
{
$new_html .= $html{$i};
}
}
}


}

if (!$stop)
{
return $new_html;
}

$l_html = substr($new_html, 0, $last_i);
$r_html = substr($new_html, $last_i, strlen ($new_html)-$last_i);

// now delete unused tags in rest string
// delete single tags
$r_html = preg_replace ("!<(img|br|hr)[^>]*>!i", "", $r_html);

// delete <tag></tag>
while (preg_match ("!<([a-z])[^>]*></\\1>!", $r_html, $m))
{
$r_html = str_replace ($m[0], "", $r_html);
}

$new_html = $l_html . '...' . $r_html;

return $new_html;
}
else
{
return $html;
}
}
else
{
return $html;
}

}

Try this one.

Brian
11-16-2004, 03:30 PM
Looks good. Any idea what the performance hit is like with this added?

Our Sponsors
 

Stefan
11-17-2004, 11:30 AM
My board is too small to benchmark the function. Perhaps on boards with more visitors. ;)

Brian
11-17-2004, 11:36 AM
I threw it up on my localhost, but never got it completely working because it's a bit of a mess and I've only got 2.0 of the CMPS on there. From what I could tell though, it didn't look like the performance hit was too bad. :)

Stefan
11-17-2004, 05:25 PM
As I said, I only tested it on my small board and it works. Btw I fixed a bug and added a premature return to the function.

Perhaps are there some coders having too much time to optimize the function. :D

Here is my board running: www.televisionboard.de

// Edit. Just a small change, moved the '...' between $l_html and $r_html ;). That looks better.

Noah
01-21-2005, 09:45 PM
Where do you put this?

Brian
01-22-2005, 10:45 AM
Somewhere near the top of your news.php file, then you replace fetch_trimmed_title with fetch_trimmed_html

TsirhCitna
08-14-2005, 12:05 AM
Is this usable for 2.0 because I couldn't find fetch_trimmed_html in news.php and being forced to have no limit for news articles as the only other solution to quotes breaking the tables is pretty lame IMO and it makes the page have far too much text.