[noparse]
<?php
function Format($str)
{
$str = htmlentities($str);
$str = bbcode_format($str);
$str = bbcode_quote($str);
$str = bbcode_img($str);
$str = find_url($str);
$str = nl2p($str);
return $str;
}
function bbcode_format($str)
{
$simple_search = array('/\[b\](.*?)\[\/b\]/is','/\[i\](.*?)\[\/i\]/is','/\[u\](.*?)\[\/u\]/is','/\[url\=(.*?)\](.*?)\[\/url\]/is');
$simple_replace = array('<b>$1</b>','<i>$1</i>','<u>$1</u>','<a href="$1" target="_blank">$2</a>');
$str = preg_replace ($simple_search, $simple_replace, $str);
return $str;
}
function bbcode_quote($str)
{
$open = '</p><p class="quote">';
$close = '</p>';
preg_match_all ('/\[quote\]/i', $str, $matches);
$opentags = count($matches['0']);
preg_match_all ('/\[\/quote\]/i', $str, $matches);
$closetags = count($matches['0']);
$unclosed = $opentags - $closetags;
for($i = 0; $i < $unclosed; $i++)
{
$str .= '</p>';
}
$str = str_replace ('[quote]', $open, $str);
$str = str_replace ('[/quote]', $close, $str);
return $str;
}
function bbcode_img($str)
{
$open = '<img src="';
$close = '" alt="" />';
preg_match_all ('/\[img\]/i', $str, $matches);
$opentags = count($matches['0']);
preg_match_all ('/\[\/img\]/i', $str, $matches);
$closetags = count($matches['0']);
$unclosed = $opentags - $closetags;
for($i = 0; $i < $unclosed; $i++)
{
$str .= '" alt="" />';
}
$str = str_replace ('[img]', $open, $str);
$str = str_replace ('[/img]', $close, $str);
return $str;
}
function nl2p($str)
{
$str = "<p>" . str_replace("\r\n","<br />", $str);
$str = str_replace("<br /><br />","</p><p>",$str) . "</p>";
$str = str_replace("<p><br /></p>","",$str);
$str = str_replace("<p></p>","",$str);
$str = str_replace("<p><p","<p",$str);
$str = str_replace("</p></p>","</p>",$str);
$str = str_replace("<p class=\"quote\">","<p class=\"quote\">\r\n\t",$str);
$str = str_replace("<p","\t\t\t\t\t" . "<p",$str);
$str = str_replace("</p>","</p>\r\n",$str);
return $str;
}
function find_url($string)
{
//"www."
$pattern_preg1 = '#(^|\s)(www|WWW)\.([^\s<>\.]+)\.([^\s\n<>]+)#sm';
$replace_preg1 = '\\1<a href="http://\\2.\\3.\\4" target="_blank">\\2.\\3.\\4</a>';
//"http://"
$pattern_preg2 = '#(^|[^\"=\]]{1})(http|HTTP|ftp|Http)(s|S)?://([^\s<>\.]+)\.([^\s<>]+)#sm';
$replace_preg2 = '\\1<a href="\\2\\3://\\4.\\5" target="_blank">\\2\\3://\\4.\\5</a>';
$string = preg_replace($pattern_preg1, $replace_preg1, $string);
$string = preg_replace($pattern_preg2, $replace_preg2, $string);
return $string;
}
?>
[/noparse]