PHP搜索http://或www的字符串

时间:2011-06-15 14:17:58

标签: php regex preg-match

  

可能重复:
  Detecting a url using preg_match? without in the string

我想搜索用户输入的字符串,对于http://的实例,如果找到一个我希望将链接包装在正确的html中,

所以例如,用户可以输入以下内容,

 Google's url is http://www.google.com,

我想要保存它,因为它,

 Google's url is <a href="http://www.google.com/">http://www.google.com</a>

这可能吗?

1 个答案:

答案 0 :(得分:3)

function make_clickable($sText) {
    $sClickText = str_replace(' www.', ' http://www.', $sText);
    $sClickText = preg_replace("/([\s])?(http|ftp|https)([\:\/\/])([^\s]+)/i", " <a href=\"$2$3$4\" target=\"_blank\">$2$3$4</a>",$sClickText);
    return $sClickText;
}

Tadaa!

相关问题