V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lslqtz
V2EX  ›  PHP

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

  •  
  •   lslqtz · 2016-10-21 03:44:15 +08:00 · 2188 次点击
    这是一个创建于 2957 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我从网上找了个 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>

    第 1 条附言  ·  2017-03-22 17:02:42 +08:00
    结贴, nl2br 后面需要跟个参数
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   6075 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 02:43 · PVG 10:43 · LAX 18:43 · JFK 21:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.