我从网上找了个 BBCode 转 HTML 的 PHP 类,但是我使用时发现换行都是<br />
我想把<br />
给换成\n<br>
,但是我在里面只找到两个,而且修改后仍然是<br />
。
<?php
error_reporting(E_ALL & ~E_DEPRECATED);
class bbcode {
private static $simple_search=array(
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[u\](.*?)\[\/u\]/is',
'/\[s\](.*?)\[\/s\]/is',
'/\[size\=(.*?)\](.*?)\[\/size\]/is',
'/\[color\=(.*?)\](.*?)\[\/color\]/is',
'/\[center\](.*?)\[\/center\]/is',
'/\[font\=(.*?)\](.*?)\[\/font\]/is',
'/\[align\=(left|center|right)\](.*?)\[\/align\]/is',
'/\[url\](.*?)\[\/url\]/is',
'/\[url\=(.*?)\](.*?)\[\/url\]/is',
'/\[mail\=(.*?)\](.*?)\[\/mail\]/is',
'/\[mail\](.*?)\[\/mail\]/is',
'/\[img\](.*?)\[\/img\]/is',
'/\[img\=(\d*?)x(\d*?)\](.*?)\[\/img\]/is',
'/\[img (.*?)\](.*?)\[\/img\]/ise',
'/\[quote\](.*?)\[\/quote\]/is',
'/\[quote\=(.*?)\](.*?)\[\/quote\]/is',
'/\[code\](.*?)\[\/code\]/is',
'/\[sub\](.*?)\[\/sub\]/is',
'/\[sup\](.*?)\[\/sup\]/is',
'/\[p\](.*?)\[\/p\]/is',
"/\[youtube\](.*?)\[\/youtube\]/i",
"/\[gvideo\](.*?)\[\/gvideo\]/i",
'/\[bull \/\]/i',
'/\[copyright \/\]/i',
'/\[registered \/\]/i',
'/\[tm \/\]/i',
);
private static $simple_replace=array(
'<strong>$1</strong>',
'<em>$1</em>',
'<u>$1</u>',
'<del>$1</del>',
'<span style="font-size: $1;">$2</span>',
'<span style="color: $1;">$2</span>',
'<div style="text-align: center;">$1</div>',
'<span style="font-family: $1;">$2</span>',
'<div style="text-align: $1;">$2</div>',
'<a rel="nofollow" href="$1">$1</a>',
'<a rel="nofollow" href="$1">$2</a>',
'<a href="mailto:$1">$2</a>',
'<a href="mailto:$1">$1</a>',
'<img src="$1" alt="" />',
'<img height="$2" width="$1" alt="" src="$3" />',
'"<img ". str_replace("'", "\"",str_replace(""", "\"", "$1")) . " src=\"$2\" />"',
'<blockquote>$1</blockquote>',
'<blockquote><strong>$1 wrote:</strong> $2</blockquote>',
'<pre>$1</pre>',
'<sub>$1</sub>',
'<sup>$1</sup>',
'<p>$1</p>',
"<object width=\"425\" height=\"350\"><embed src=\"https://www.youtube.com/v/$1\" type=\"application/x-shockwave-flash\" width=\"425\" height=\"350\"></embed></object>",
"<embed src=\"https://video.google.com/googleplayer.swf?docId=$1\" type=\"application/x-shockwave-flash\" style=\"width: 425px; height: 350px;\">",
'•',
'©',
'®',
'™',
);
private static $lineBreaks_search=array(
'/\[list(.*?)\](.+?)\[\/list\]/sie',
'/\[\/list\]\s*\<br\>/i',
'/\[code\](.+?)\[\/code\]/sie',
'/\[\/code\]\s*\<br\>/i',
'/\[\/quote\]\s*\<br\>/i',
'/\[\/p\]\s*\<br\>/i',
'/\[\/center\]\s*\<br\>/i',
'/\[\/align\]\s*\<br\>/i',
);
private static $lineBreaks_replace=array(
"'[list$1]'.str_replace('<br>', '', '$2').'[/list]'",
"[/list]",
"'[code]'.str_replace('<br>', '', '$1').'[/code]'",
"[/code]",
"[/quote]",
"[/p]",
"[/center]",
"[/align]",
);
private static function process_list_items($list_items) {
$result_list_items=array();
preg_match_all("/\[li\](.*?)\[\/li\]/is", $list_items, $li_array);
$li_array=$li_array[1];
if (empty($li_array)) {
$list_items_array=explode("[*]", $list_items);
foreach ($list_items_array as $li_text) {
$li_text=trim($li_text);
if (empty($li_text)) {
continue;
}
$li_text=nl2br($li_text);
$result_list_items[]='<li>'.$li_text.'</li>';
}
} else {
foreach ($li_array as $li_text) {
$li_text=nl2br($li_text);
$result_list_items[]='<li>'.$li_text.'</li>';
}
}
$list_items=implode("\n", $result_list_items);
return $list_items;
}
public static function parse($string) {
$s=(string) $string;
if (empty($s)) {
return '';
}
$s=preg_replace('/\[url\=([^( http)].+?)\](.*?)\[\/url\]/i', '[url=http://$1]$2[/url]', $s);
$s=preg_replace('/\[url\]([^( http)].+?)\[\/url\]/i', '[url=http://$1]$1[/url]', $s);
$s=nl2br($s);
$s=preg_replace(self::$lineBreaks_search,self::$lineBreaks_replace, $s);
$s=preg_replace(self::$simple_search, self::$simple_replace, $s);
$s=preg_replace('/\[list\](.*?)\[\/list\]/sie', '"<ul>\n".self::process_list_items("$1")."\n</ul>"', $s);
$s=preg_replace('/\[list\=(disc|circle|square|decimal|decimal-leading-zero|lower-roman|upper-roman|lower-greek|lower-alpha|lower-latin|upper-alpha|upper-latin|hebrew|armenian|georgian|cjk-ideographic|hiragana|katakana|hiragana-iroha|katakana-iroha|none)\](.*?)\[\/list\]/sie',
'"<ol style=\"list-style-type: $1;\">\n".self::process_list_items("$2")."\n</ol>"', $s);
return $s;
}
}
echo bbcode::parse('[b]Test[/b]
')
?>
输出的结果是<strong>Test</strong><br />
,我想使它他转成
<strong>Test</strong>
<br>