php匹配字符串URL并替换为超链接

<?php

$regex = '@(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))@';

var_dump( preg_match($regex, 'http://segmentfault.com/q/1010000000584340') ); //true

若想自动添加<a>标签则执行

$content = preg_replace($preg, '<a href="$1">$1</a>', $content);

猜你喜欢

转载自blog.csdn.net/yuexiaxiaoxi27172319/article/details/51898687