将regex更改为不包括[和]中的内容


change regex to not include things inside [ and ]

我这里有这个自动链接的正则表达式代码:

// turn any url into url bbcode that doesn't have it already - so we can auto link urls- thanks stackoverflow
$URLRegex = '/(?:(?<!('['/url']|'['/url=))('s|^))'; // No [url]-tag in front and is start of string, or has whitespace in front
$URLRegex.= '(';                                    // Start capturing URL
$URLRegex.= '(https?|ftps?|ircs?):'/'/';            // Protocol
$URLRegex.= ''S+';                                  // Any non-space character
$URLRegex.= ')';                                    // Stop capturing URL
$URLRegex.= '(?:(?<![.,;!?:'"''()-])('/|'s|'.?$))/i';      // Doesn't end with punctuation and is end of string, or has whitespace after
$body = preg_replace($URLRegex,"$2[url=$3]$3[/url]$5", $body);

问题是,如果url是在引号标签和结束引号标签是对着链接结束引号标签被包括在链接,这当然搞砸了一切!

我怎么能调整正则表达式不包括任何内部的[和]在链接?

样本输入:

[quote=liamdawe] Have you had a look at [url=http://pcgamingwiki.com/wiki/Serious_Sam_II#Linux_Installation]this howto[/url]? :)
http://pcgamingwiki.com/wiki/Serious_Sam_II#Linux_Installation[/quote]
Testing

正确的输出应该是:

<div class="quote"><strong>Quote from liamdawe</strong><br />  Have you had a look at <a href="http://pcgamingwiki.com/wiki/Serious_Sam_II#Linux_Installation" target="_blank">this howto</a>? <img src="/jscripts/sce/emoticons/smile.png" alt="" /><br />
<br />
<a href="http://pcgamingwiki.com/wiki/Serious_Sam_II#Linux_Installation" target="_blank">http://pcgamingwiki.com/wiki/Serious_Sam_II#Linux_Installation</a></div><br />
Testing
但是我得到的输出是:
<div class="quote"><strong>Quote from liamdawe</strong><br />  Have you had a look at <a href="http://pcgamingwiki.com/wiki/Serious_Sam_II#Linux_Installation" target="_blank">this howto</a>? <img src="/jscripts/sce/emoticons/smile.png" alt="" /><br />
<br />
<a href="http://pcgamingwiki.com/wiki/Serious_Sam_II#Linux_Installation </div>" target="_blank">http://pcgamingwiki.com/wiki/Serious_Sam_II#Linux_Installation[/quote]</a><br />
Testing<br />

正如你所看到的,它在链接中包含了[/quote]标签,因为它没有忽略自动链接器正则表达式中的bbcode标签。

如果需要的话,下面的代码也可以执行这种类型的引用://引用一个真实的人、书或其他东西$pattern = '/[quote'=(.+?)](.+?)[/quote]/is';

$replace = "<div class='"quote'"><strong>Quote from $1</strong><br />$2</div>";
while(preg_match($pattern, $body))
{
    $body = preg_replace($pattern, $replace, $body);
}

TRY this

$URLRegex = '/(?:(?<!('['/url']|'['/url=))('s|^))'; // No [url]-tag in front and is start of string, or has whitespace in front
$URLRegex.= '(';                                    // Start capturing URL
$URLRegex.= '(https?|ftps?|ircs?):'/'/';            // Protocol
$URLRegex.= '['w'd'.'/#'_'-'?:=]+';                        // Any non-space character
$URLRegex.= ')';                                    // Stop capturing URL
$URLRegex.= '(?:(?<![.,;!?:'"''()-])('/|'[|'s|'.?$))/i';      // Doesn't end with punctuation and is end of string, or has whitespace after
$body = preg_replace($URLRegex,"$2[url=$3]$3[/url]$5", $body);