【发布时间】:2015-03-05 22:21:12
【问题描述】:
当我运行解析时,它会中断图像以通过 Oembed 系统将 URL 替换为链接。如何编辑此正则表达式,使其不会捕获 BBCode 括号内的链接?
// Parse bbcodes before link parsing for image support
$text = self::parseBBCodes($text);
$text = preg_replace_callback('/(https?:\/\/.*?)(\s|$)/i', function ($match) use (&$oembedCount, &$maxOembedCount) {
我已经试过了
$text = preg_replace_callback('/(?<!\])(https?:\/\/.*?)(\s|$)(?!\[)/i', function ($match) use (&$oembedCount, &$maxOembedCount) {
这似乎可行,但图像没有被转换。虽然常规的 bbcode 是。
BBCode函数
/**
* Parse BBCode
*
* @param string $text contains the text with BBCode to be parsed
*/
public static function parseBBcodes($text)
{
// BBcode array
$find = array(
'~\[b\](.*?)\[/b\]~s',
'~\[i\](.*?)\[/i\]~s',
'~\[u\](.*?)\[/u\]~s',
'~\[quote\](.*?)\[/quote\]~s',
'~\[size=(.*?)\](.*?)\[/size\]~s',
'~\[color=(.*?)\](.*?)\[/color\]~s',
'~\[img\](https?://.*?\.(?:jpg|jpeg|gif|png|bmp))\[/img\]~s'
);
// HTML tags to replace BBcode
$replace = array(
'<b>$1</b>',
'<i>$1</i>',
'<span style="text-decoration:underline;">$1</span>',
'<pre>$1</'.'pre>',
'<span style="font-size:$1px;">$2</span>',
'<span style="color:$1;">$2</span>',
'<img src="$1" alt="" />'
);
// Replacing the BBcodes with corresponding HTML tags
return preg_replace($find,$replace,$text);
}
【问题讨论】: