PHP将URL转换为字符串中的链接

时间:2013-10-04 14:37:58

标签: php regex

$string1 = "You can view the full calendar at 
           http://www.something.org/calendar/57682117 
           or email: info@something.org"
$string2 = "You can view the full calendar at 
           www.something.org/calendar/57682117 
           or email: info@something.org"

这是我的字符串。我需要做的是将两个网址转换为链接到网页的链接,然后需要将电子邮件转换为mailto:links。

我不需要考虑blahblah.com的网站。他们将永远拥有这些www之一。或http(s)。我对正则表达式很糟糕......所以请帮助我们!

1 个答案:

答案 0 :(得分:0)

$output = preg_replace('_<a href="(?!mailto:)(?!tel:)[^"]+[^>]*>(.+?)</a>_', '$1', $string1);
echo '<pre>'.print_r(htmlentities($output), true).'</pre>';