【发布时间】:2013-06-17 15:51:21
【问题描述】:
Tehre 似乎不是如何以最佳方式做到这一点的明确答案。
我有一些 bbcode 可能有 bbcode 格式的链接:
[url=http://thisisalink.com]链接[/url]
以及可能的复制/粘贴网址:
我想用可点击的链接替换这两个实例。我目前有以下内容:正在运行的正则表达式:
"/\[link=http:\/\/(.*?)\](.*?)\[\/link\]/is"
"/\[link=https:\/\/(.*?)\](.*?)\[\/link\]/is"
"/\[link=(.*?)\](.*?)\[\/link\]/is"
$URLRegex = '/(?:(?<!(\[\/link\]|\[\/link=))(\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
似乎我无法让两者都工作。在这种情况下,最后一个正则表达式似乎取消了第一个正则表达式的链接。
当然,这已经在某处记录了将 bbcode 链接和粘贴的 URL 链接在一起而不会相互冲突的最佳方式。
【问题讨论】: