我有這種自動鏈接的正則表達式代碼在這裏:變化的正則表達式不包括裏面的東西[和]
// 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]標記,因爲它不會忽略auto鏈接器正則表達式中的bbcode標記。
這裏是做這種類型的引用的代碼以及如果需要的話: //引用一個實際的人,書或任何 $模式=「/[quote\=(.+?)](.+? )[/報價] /是;
$replace = "<div class=\"quote\"><strong>Quote from $1</strong><br />$2</div>";
while(preg_match($pattern, $body))
{
$body = preg_replace($pattern, $replace, $body);
}
向我們展示代碼之前和之後,請。 – silkfire
這不相關。這是鏈接的所有代碼。 – NaughtySquid
然後它不是給你任何幫助。 – silkfire