1, 'nl2br' => 1, 'url_parsing' => 1, 'smileys' => 1); // By default, apply all filtering
function bbcode_to_html() {
// ***************************************************************
// This function parses BBCode into HTML.
// ***************************************************************
$pos = strpos($this->string, '[');
while ($pos !== FALSE) {
$len = strpos($this->string, ']', $pos + 1) - $pos + 1;
if ($len) { // No need to continue is there is no ]
$rest = explode('=', substr($this->string, $pos + 1, $len - 2), 2);
$tag = $rest[0];
if (($pos2 = strpos($this->string, "[/$tag]", $pos + $len)) === FALSE) { // No end tag
$tag = NULL;
} else {
$param_name = substr($this->string, $pos + $len, $pos2 - $pos - $len);
$len2 = strpos($this->string, ']', $pos2 + 1) - $pos2 + 1;
}
switch($tag) {
case 'i':
case 'u':
case 'b':
case 's':
$this->string = substr_replace($this->string, "<$tag>", $pos, $len);
$this->string = substr_replace($this->string, "$tag>", $pos2, $len + 1);
break;
case 'img':
$replace_str = '';
$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
break;
case 'url':
if (!$rest[1])
$rest[1] = $param_name;
$target = NULL;
if (substr($rest[1], 0, 7) == 'http://')
$target = ' target="_blank"';
if ($param_name)
$replace_str = '' . $param_name . '';
else
$replace_str = '' . $rest[1] . '';
$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
break;
case 'align':
$replace_str = '
' . $param_name . '
';
$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
break;
case 'list':
$pos_del = strpos($param_name, '[*]');
$param_name = substr_replace($param_name, '', 0, $pos_del + 3); // Ignore first [*]
$replace_str = '- ';
$replace_str .= str_replace('[*]', '
- ', $param_name);
$replace_str .= '
';
$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
break;
case 'code':
case 'quote':
$replace_str = '' . $param_name . '
';
$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
break;
case 'anchor':
$replace_str = '';
$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
break;
default:
// Prevents infinite loops should the string contain "[" char without it being a BBCode
$pos++;
break;
}
}
// Repeat
$pos = strpos($this->string, '[', $pos);
}
}
function html_to_bbcode() {
// ***************************************************************
// This function parses HTML into BBCode.
// ***************************************************************
$open_tags = array ();
$pos = strpos($this->string, '<');
while ($pos !== FALSE) {
$len = strpos($this->string, '>', $pos + 1) - $pos + 1;
if ($len) { // No need to continue is there is no >
$rest = explode('=', substr($this->string, $pos + 1, $len - 2), 2);
$tag = explode(' ', $rest[0]);
$tag = $tag[0];
if (($pos2 = strpos($this->string, "$tag>", $pos + $len)) === FALSE) { // No end tag
// Let's see if it's a tag without end tag, i.e.
, or
$ok_tags = array ('');
if (!in_array(substr($this->string, $pos, $len), $ok_tags) && strpos($this->string, '/>', $pos + 1) === FALSE)
$rest[0] = NULL;
} else {
$param_name = substr($this->string, $pos + $len, $pos2 - $pos - $len);
$len2 = strpos($this->string, '>', $pos2 + 1) - $pos2 + 1;
}
switch($rest[0]) {
case 'i':
case 'u':
case 'b':
case 's':
$this->string = substr_replace($this->string, "[$tag]", $pos, $len);
$this->string = substr_replace($this->string, "[/$tag]", $pos2, $len + 1);
break;
case 'a href':
$link = explode('"', $rest[1]);
$replace_str = '[url=' . $link[1] . ']' . $param_name . '[/url]';
$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
break;
case 'a name':
$link = explode('"', $rest[1]);
$replace_str = '[anchor]' . $link[1] . '[/anchor]';
$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
break;
case 'div align':
$link = explode('"', $rest[1]);
array_push($open_tags, 'align');
$replace_str = '[align=' . $link[1] . ']' . $param_name;
$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 - $pos);
break;
case 'div class':
$link = explode('"', $rest[1]);
array_push($open_tags, $link[1]);
$replace_str = '[' . $link[1] . ']' . $param_name;
$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 - $pos);
break;
case '/div':
if (count($open_tags) > 0)
$replace_str = '[/' . array_pop($open_tags) . ']';
else
$replace_str = '';
$this->string = substr_replace($this->string, $replace_str, $pos, 6);
break;
case 'img src':
$link = explode('"', $rest[1]);
$replace_str = '[img]' . $link[1] . '[/img]';
$this->string = substr_replace($this->string, $replace_str, $pos, $len);
break;
case 'br /':
$replace_str = '';
$this->string = substr_replace($this->string, $replace_str, $pos, $len);
break;
case 'ul':
$replace_str = '[list]' . $param_name . '[/list]';
$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
break;
case 'li':
$replace_str = '[*]' . $param_name;
$this->string = substr_replace($this->string, $replace_str, $pos, $pos2 + $len2 - $pos);
break;
default:
// Prevents infinite loops should the string contain "<" char without it being html
$pos++;
break;
}
}
// Repeat
$pos = strpos($this->string, '<', $pos);
}
// Close any open tags, in case the html contains errors
if (count($open_tags) > 0) {
foreach($open_tags as $value) {
$this->string .= '[/' . array_pop($open_tags) . ']';
}
}
}
function html_filter() {
// ***************************************************************
// This function provides basic html filtering.
// By default, all rules are applied; should you require to
// disable some rules, the html_filter_prefs array can be used.
//
// Ex: $my_class->html_filter_prefs['smileys'] = 0; // No smileys
// ***************************************************************
// First we make sure there is not html
if ($this->html_filter_prefs['entities'])
$this->string = htmlspecialchars($this->string, ENT_NOQUOTES);
// Smileys
if ($this->html_filter_prefs['smileys']) {
$smileys = array (':)' => 0, ':-)' => 0, ';)' => 1, ';-)' => 1, ':D' => 2, ':-D' => 2, ':P' => 3, ':-P' => 3, ':p' => 3, ':-p' => 3, ':(' => 4, ':-(' => 4);
$filename = array('happy.gif', 'wink.gif', 'biggrin.gif', 'tongue.gif', 'sad.gif');
foreach ($smileys as $key => $value) {
$img_url = '';
$this->string = str_replace($key, $img_url, $this->string);
}
}
// This automatically converts new lines and tabs
if ($this->html_filter_prefs['nl2br']) {
$this->string = nl2br($this->string);
$this->string = str_replace("\t", ' ', $this->string);
}
// Automatic URL parsing
if ($this->html_filter_prefs['url_parsing']) {
// For 'http://example.com/?id=' type links
$this->string = ereg_replace('[ ]+([a-z]+://[-a-z0-9A-Z&_\/\.\?\=]+)[ ]+', ' \\1 ', $this->string);
// For 'www.example.com' type links
$this->string = ereg_replace('[ ]+(www\.[-a-z0-9A-Z\.]+\.[-a-z0-9A-Z&_\/\.\?\=]+)[ ]+', ' http://\\1 ', $this->string);
}
}
}
?>