这里是另一个解决方案,这将捕获所有http / https / www并转换为可点击的链接。
$url = '~(?:(https?)://([^\s
$string = preg_replace($url, '$0', $string);
echo $string;
或者,为了只捕获http / https,然后使用下面的代码。
$url = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/';
$string= preg_replace($url, '$0', $string);
echo $string;
编辑:
以下脚本将捕获所有网址类型,并将其转换为可点击的链接。
$url = '@(http)?(s)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])@';
$string = preg_replace($url, '$0', $string);
echo $string;