将 BBCode [IMG] 转换为 <img>

Convert BBCode [IMG] to <img>

我正在使用 preg_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></strong>',
    '<em></em>',
    '<u></u>',
    '<ul></ul>',
    '<li></li>',
    '<a href="../login/profile?u=" target="_blank"></a>',
    '<a href="" target="_blank"></a>',
    '<a href="" target="_blank"></a>',
    '<img src=""></img>',
    '<quote></quote>',
    '<code></code>'
);

$string = preg_replace($search, $replace, $string);

现在它创建了一个带有 link 的图像标签,但将 ] 添加到 link 的开头和结尾,因此它无法正确显示图像。

编辑:

这是因为我将 links 转换为锚标签,它在 [img] BBCode 中存在冲突。

$url = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/';

$string = preg_replace($url, '[url=[=12=]][=12=][/url]', $string);

几点说明:

  • 将您的模式定界符从 / 更改为 ~,这样您就不必转义模式中存在的 / 个字符。
  • 不要在您不打算使用的子字符串上使用捕获组。在您的模式中,none 不需要的子字符串需要用括号括起来以维护模式逻辑。
  • 如果字符 class 被打开,正则表达式引擎只会将 ] 视为字符 class 的结尾。因此,我没有转义任何 ] 个字符。
  • 因为您没有使用任何锚点(^$),所以不需要 m 模式修饰符。
  • 如果您希望模式不区分大小写,请使用 i 模式 flag/modifier。
  • 去除所有多余的捕获组后,只需要在替换字符串中使用</code>即可。</li> <li>处理 bbcoded url 后,对所有尚未转换为 html 元素的 url 进行最后一次扫描。 <code>(*SKIP)(*FAIL) "disqualifies" 这些不需要的子字符串。

代码:(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="" target="_blank"></a>',
    '<a href="" target="_blank"></a>',
    // ...
    '<img src=""></img>',
    // ...
    '<a href="[=10=]" target="_blank">[=10=]</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>',
)