function bb_decode($data) {
$data = htmlspecialchars($data);
$offset = 0;
while(($pos = strpos($data, '[noparse]', $offset)) !== false) {
$end = strpos($data, '[/noparse]', $pos);
if($end === false) break;
$noparse = substr($data, $pos + 9, $end - ($pos + 9));
$noparse = str_replace(array('[', ']'), array('[', ']'), $noparse);
$end += 10;
$data = substr_replace($data, $noparse, $pos, $end - $pos);
$offset = $end;
}
$search[] = '[b]';
$search[] = '[/b]';
$search[] = '[i]';
$search[] = '[/i]';
$search[] = '[u]';
$search[] = '[/u]';
$search[] = '[/color]';
$search[] = '[/size]';
$search[] = '[/font]';
$search[] = '[highlight]';
$search[] = '[/highlight]';
$replace[] = '<strong>';
$replace[] = '</strong>';
$replace[] = '<i>';
$replace[] = '</i>';
$replace[] = '<u>';
$replace[] = '</u>';
$replace[] = '</font>';
$replace[] = '</font>';
$replace[] = '</font>';
$replace[] = '<span class="highlight">';
$replace[] = '</span>';
$data = str_replace($search, $replace, $data);
$search = array();
$replace = array();
$search[] = '/\[color=(.*?)\]/';
$search[] = '/\[size=(.*?)\]/';
$search[] = '/\[font=(.*?)\]/';
$search[] = '/\[email\](.*?)\[\/email\]/';
$search[] = '/\[email=(.*?)\](.*?)\[\/email\]/';
$search[] = '/\[url\](.*?)\[\/url\]/';
$search[] = '/\[url=(.*?)\](.*?)\[\/url\]/';
$search[] = '/\[img\](.*?)\[\/img\]/';
$replace[] = '<font color="${1}">';
$replace[] = '<font size="${1}">';
$replace[] = '<font face="${1}">';
$replace[] = '<a href="mailto:${1}">${1}</a>';
$replace[] = '<a href="mailto:${1}">${2}</a>';
$replace[] = '<a href="${1}">${1}</a>';
$replace[] = '<a href="${1}">${2}</a>';
$replace[] = '<img src="${1}" />';
$data = preg_replace($search, $replace, $data);
$offset = 0;
while(strpos($data, '[list]', $offset) !== false) {
$pos = strpos($data, '[list]', $offset);
$end = strpos($data, '[/list]', $pos);
if($end === false) break;
$list = trim(substr($data, $pos + 6, $end - ($pos + 6)));
$elements = explode('[*]', $list);
$list = '<ul>';
for($i = 1; $i < count($elements); $i++) {
$list .= '<li>'.nl2br(rtrim($elements[$i])).'</li>';
}
$list .= '</ul>';
$end += 7;
$offset = $end;
$data = substr_replace($data, $list, $pos, $end - $pos);
}
$offset = 0;
while(strpos($data, '[list=', $offset) !== false) {
$pos = strpos($data, '[list=', $offset);
$pos2 = strpos($data, ']', $pos + 6);
$type = substr($data, $pos + 6, $pos2 - ($pos + 6));
$end = strpos($data, '[/list]', $pos2);
if($end === false) break;
$list = trim(substr($data, $pos2 + 1, $end - ($pos2 + 1)));
$elements = explode('[*]', $list);
$list = '<ol type="'.$type.'">';
for($i = 1; $i < count($elements); $i++) {
$list .= '<li>'.nl2br(rtrim($elements[$i])).'</li>';
}
$list .= '</ol>';
$end += 7;
$offset = $end;
$data = substr_replace($data, $list, $pos, $end - $pos);
}
$data = nl2br($data);
$offset = 0;
while(($pos = strpos($data, '[code]', $offset)) !== false) {
$end = strpos($data, '[/code]', $pos);
if($end === false) break;
$code = substr($data, $pos + 6, $end - ($pos + 6));
$strlen = strlen($code) + 13;
$code = '<pre>'.str_replace('', '', $code).'</pre>';
$end += 7;
$offset = $end - ($strlen - strlen($code));
$data = substr_replace($data, $code, $pos, $end - $pos);
}
return $data;
}