代码之家  ›  专栏  ›  技术社区  ›  TheWelshManc

将BBCode[IMG]转换为<IMG>

  •  2
  • TheWelshManc  · 技术社区  · 7 年前

    我正在使用preg\u replace将所有BBCode转换为HTML,但无法使img标记正常工作。这是我的密码:

    $search = array (
        '/(\[b\])(.*?)(\[\/b\])/m',
        '/(\[i\])(.*?)(\[\/i\])/m',
        '/(\[u\])(.*?)(\[\/u\])/m',
        '/(\[ul\])(.*?)(\[\/ul\])/m',
        '/(\[li\])(.*?)(\[\/li\])/m',
        '/(\[user=)(.*?)(\])(.*?)(\[\/user\])/m',
        '/(\[url=)(.*?)(\])(.*?)(\[\/url\])/m',
        '/(\[url\])(.*?)(\[\/url\])/m',
        '/(\[img=)(.*?)(\])(.*?)(\[\/img\])/m',
        '/(\[quote\])(.*?)(\[\/quote\])/m',
        '/(\[code\])(.*?)(\[\/code\])/m',
    );
    
    $replace = array (
        '<strong>$2</strong>',
        '<em>$2</em>',
        '<u>$2</u>',
        '<ul>$2</ul>',
        '<li>$2</li>',
        '<a href="../login/profile?u=$2" target="_blank">$2</a>',
        '<a href="$2" target="_blank">$4</a>',
        '<a href="$2" target="_blank">$2</a>',
        '<img src="$4"></img>',
        '<quote>$2</quote>',
        '<code>$2</code>'
    );
    
    $string = preg_replace($search, $replace, $string);
    

    ] 到链接的开头和结尾,这样它就不能正确显示图像。

    编辑:

    这是因为我将链接转换为锚定标记,而它在[img]BBCode中是冲突的。

    $url = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/';
    
    $string = preg_replace($url, '[url=$0]$0[/url]', $string);
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   mickmackusa Tom Green    7 年前

    • 将模式分隔符从 / ~ 这样你就不用逃避 存在于模式中的字符。
    • 正则表达式引擎只考虑 ] ] 角色。
    • 因为你没有使用任何锚( ^ $ ),的 m 图案修改器是不必要的。
    • i 图案标志/修改器。
    • 删除所有多余的捕获组后,只需使用 $1 在替换字符串中。
    • (*SKIP)(*FAIL) “取消”这些不需要的子字符串。

    Demo )

    $bbcodes = [
        'Look at this:https://www.example.com/example?ohyeah=sure#okay this is a raw link',
        'No attibute bbcode url: [url]http://example.com/x1[/url]',
        'A url with link and link text: [url=http://example.com/x2]x2[/url]',
        'Image with "ignorable" text: [IMG=sumpthing.jpg]sumpthing[/IMG]',
        'Image: [img=sumpinelse][/img]'
    ];
    
    $search = array (
        // ...
        '~\[url=((?:ht|f)tps?://[a-z\d.-]+\.[a-z]{2,3}/\S*?)](.*?)\[/url]~i',
        '~\[url]((?:ht|f)tps?://[a-z\d.-]+\.[a-z]{2,3}/\S*?)\[/url]~i',
        // ...
        '~\[img=(.*?)].*?\[/img]~i',  // if you want the possibility of dot matching newlines, add s pattern modifier
        // ...
        '~(?:<a.*?</a>|<img.*?</img>)(*SKIP)(*FAIL)|\bhttps?://.+?(?=\s|$)~im'  // mop up any remaining links that are not bbtagged
    );
    $replace = array (
        // ...
        '<a href="$1" target="_blank">$2</a>',
        '<a href="$1" target="_blank">$1</a>',
        // ...
        '<img src="$1"></img>',
        // ...
        '<a href="$0" target="_blank">$0</a>',
    );
    var_export(preg_replace($search, $replace, $bbcodes));
    

    array (
      0 => 'Look at this:<a href="https://www.example.com/example?ohyeah=sure#okay" target="_blank">https://www.example.com/example?ohyeah=sure#okay</a> this is a raw link',
      1 => 'No attibute bbcode url: <a href="http://example.com/x1" target="_blank">http://example.com/x1</a>',
      2 => 'A url with link and link text: <a href="http://example.com/x2" target="_blank">x2</a>',
      3 => 'Image with "ignorable" text: <img src="sumpthing.jpg"></img>',
      4 => 'Image: <img src="sumpinelse"></img>',
    )
    
    推荐文章