I am using a regex to replace all email addresses in a string with a nice <a> to make them clickable. This works perfect, except for the case when there are two words of a certain minimum length and a dash between them in front of the email address. Only then I get an empty string as result.
<?php
$search = '#(^|[ \n\r\t])(([a-z0-9\-_]+(\.?))+@([a-z0-9\-]+(\.?))+[a-z]{2,5})#si';
$replace = '\\1<a href="mailto:\\2">\\2</a>';
$string = "tttteeee-sssstttt [email protected]";
echo preg_replace($search, $replace, $string);
// Output: "" (empty)
$string = "te-st [email protected]";
echo preg_replace($search, $replace, $string);
// Output: "te-st <a href="mailto:[email protected]">[email protected]</a>" (as expected)
$string = "[email protected] tttteeee-sssstttt";
echo preg_replace($search, $replace, $string);
// Output: "<a href="mailto:[email protected]">[email protected]</a> tttteeee-sssstttt" (as expected)
?>
I have tried everything, but I really can't find the problem. A solution would be removing the first dash in the regex (before the @ sign), but that way email addresses with a dash before the @ wouldn't be highlighted.
{1,}==+right?+is much clearer!NULLhere....