以下正则表达式:
$common_tlds = 'us|me|com|net|org|ly|be|edu|gov|uk|ca|de|jp|fr|au|ru|ch|it|nl|se|no|es|mil|co';
$regex = '#(?:https?://)?([^.\s]+(?:[^\s.]|[^\s][^\s.])*\.(?:' . $common_tlds . ')[^.\s]*)#i';
在这里使用:
preg_replace($regex,'<a href="http://$1" target="_blank">$1</a>', $text);
在非常短的文字上给我一个PREG_BACKTRACK_LIMIT_ERROR
。一个示例文本是:
Life cant always give you the best shoes,handbags,clothes but it can give you the best creations.
我知道,有更好的链接在线查找正则表达式,但我很好奇我的正则表达式导致大规模回溯以及如何改进它。谢谢!
答案 0 :(得分:1)
尝试这样的事情:
$regex = '#(?:https?://)?([^.\s]+(?:\.[^.\s]+)*\.(?:' . $common_tlds . '))#i';
评论:
[^.\s]+(?:\.[^.\s]+)*
只允许一种匹配域的方式,在点处锚定。 [^.\s]*
- 从最后删除,不确定在tld之后它应该做什么。如果您想匹配.co.il
等域名,则可能需要另一个点:(?:\.[^.\s]+)?
\b
,以避免匹配(google.com
。