Thanks to the code posted here by dannys, I now have UBB style tags in my website.
However, I have one small problem remaining.
On the front page of my website, a section of the latest thread is displayed, either the first 197 characters, or up to the first line break - whichever comes first. A link to the entire post is then added.
However, if I have an open tag, say [url], near position 197 in the string, the closing tag gets cut off and the formatting gets screwed up. Therefor the only solution seems to be to automatically close any open tags before the link is appended to the end of the post.
So, can anyone help??
<?php
//UBB Code Replacement Function
$tagList['image']=Array('start'=>'<img src="','end'=>'">');
$tagList['b']=Array('start'=>'<b>','end'=>'</b>');
$tagList['i']=Array('start'=>'<i>','end'=>'</i>');
$tagList['link']=Array('start'=>'<a class="small" target="_blank" href="','end'=>'">\\1</a>');
$tagList['email']=Array('start'=>'<a class="small" href="mailto:','end'=>'">\\1</a>');
$tagList['link=(.*)']=Array('start'=>'<a class="small" target="_blank" href="','end'=>'">\\2</a>');
$tagList['email=(.*)']=Array('start'=>'<a class="small" href="mailto:','end'=>'">\\2</a>');
function processCode($text,$tagList)
{
foreach ($tagList as $tagName=>$replace)
{
$tagEnd=preg_replace('/\W/Ui','',$tagName);
$text=preg_replace("|\[$tagName\](.*)\[/$tagEnd\]|Ui","$replace[start]\\1$replace[end]",$text);
}
return $text;
}
//Database queried here....
//Trim down the section of text to 197 characters
$body=trim(substr(stripslashes(nl2br($row[4])),0,197));
if (strlen($row[4]) > 197)
{
//Check to see if the remaining string contains any breaks that may
//throw out the formatting of the page.
$pos1 = strpos(nl2br($body), '<br />');
if ($pos1)
{
//If it does, shorten the string down to the point before the break occurs....
$body = substr($body,0,$pos1-1);
}
//...then append the "...[more]" link to the end.
$body .= "... <a class=\"small\" href=\"allmessages.php#$row[0]\" target=\"mainwindow\">[more]</a>";
}
//Replace the UBB Code with the corresponding html
$body=processCode($body,$tagList);
echo $body;