The following regex:
$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';
used here:
preg_replace($regex,'<a href="http://$1" target="_blank">$1</a>', $text);
Is giving me a PREG_BACKTRACK_LIMIT_ERROR on very short text. One example text is:
Life cant always give you the best shoes,handbags,clothes but it can give you the best creations.
I know, there are better link finding regex online, but I was curious about what in my regex was causing massive backtracking and how I could improve it. Thanks!
Try something like this:
Comments:
[^.\s]+(?:\.[^.\s]+)*allows only one way of matching the domain, anchoring at dots.[^.\s]*– Removed from the end, not sure what it suppose to do after the tld. If you want to match domains like.co.il, you may want another dot there:(?:\.[^.\s]+)?\bat the start, to avoid matching(google.com.