请教个关于 BBCode 转 HTML 的问题。

2016-10-21 03:44:15 +08:00
 lslqtz

我从网上找了个 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("&#039;", "\"",str_replace("&quot;", "\"", "$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;\">",
'&bull;',
'&copy;',
'&reg;',
'&trade;',
);
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>

2190 次点击
所在节点    PHP
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/314299

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX