我目前有这段代码:
// 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.= '(?:(?<![[:punct:]])(\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);
哪个查找任何网址,并将它们转换为bbcode(基本上是自动链接网址),问题是如果有一个/的结尾,它就不会被解析。
有人可以告诉我如何解决这个问题吗?谢谢!
答案 0 :(得分:2)
否定后卫中的[[:punct:]]
将匹配/
,从而阻止其包含在匹配中。
您可以将[[:punct:]]
替换为包含您希望阻止成为最后一个字符的所有特定字符的字符类,例如[.,;!?:\"\'()-]
。