PHP preg_replace from beginning of word


PHP preg_replace from beginning of word

我正在尝试运行preg_replace或str_replace来包装<a>标签中的url -这是我所拥有的一个示例:

$string = "this is my website: mysite.com and this is my email: name@mysite.com";
$link = 'mysite.com';
echo str_replace($link, '<a>'.$link.'</a>', $string);

结果如下:

this is my website: <a>mysite.com</a> and this is my email: name@<a>mysite.com</a>

显然我不想结束电子邮件地址,有没有一种方法,我可以运行替换只从单词的开始,忽略任何结果在其他字符的中间?

试试这个:

echo preg_replace_all('/''s(mysite''.com)/i','<a>$1</a>',$string);

你可以在这里找到更多关于正则表达式的信息