1

我目前有这个代码:

// 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);

它查找任何 url,并将它们转换为 bbcode(基本上自动链接 url),问题是如果一个 / 在它的末尾,它不会被解析。

有人可以告诉我如何解决这个问题吗?谢谢!

4

1 回答 1

2

[[:punct:]]否定后视中的 将匹配,/从而防止其包含在匹配中。

您可以将 替换为[[:punct:]]包含要防止成为最后一个字符的所有特定字符的字符类,例如[.,;!?:\"\'()-].

于 2013-03-14T15:22:06.713 回答