这里,试试这个:
$status_text = preg_replace('|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '$1 <a href="$2">$3</a>', $status_text);
echo $status_text;
或使其easer一点点阅读:
$m = '|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
$r = '$1 <a href="$2">$3</a>';
$status_text = preg_replace($m,$r,$status_text);
echo $status_text;
编辑 - 更新由于OP新的信息 -
你的#标签的正则表达式没有考虑到在URL中考虑散列,所以我们进行了修复... 此外,你应该匹配之前匹配的网址哈希标签或加标签,因为否则就会乱了你的哈希标签创建链接并加标签
$status_text = preg_replace('|(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '<a href="$1">$2</a>', $status_text);
$status_text = preg_replace('|\B#([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
$status_text = preg_replace('|\B\+([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
还是要使它更容易一点阅读...
$match_href = '|(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
$match_hash = '|\B#([\d\w_]+)|i';
$match_plus = '|\B\+([\d\w_]+)|i';
$replace_url = '<a href="$1">$2</a>';
$replace_tag = '<a href="http://url.com/pluslink/$1">$0</a>';
$status_text = preg_replace($match_href, $replace_url, $status_text);
$status_text = preg_replace($match_hash, $replace_tag, $status_text);
$status_text = preg_replace($match_plus, $replace_tag, $status_text);
再次编辑 - 添加可能会有所帮助的URL -
您可以测试出的正则表达式在这里:http://gskinner.com/RegExr/
ANOTHER编辑
按照评论/问题,如果你想要解释缺少http协议的网址,请使用以下内容:
$status_text = preg_replace('|((https?://)?([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '<a href="$1">$3</a>', $status_text);
$status_text = preg_replace('|\B#([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
$status_text = preg_replace('|\B\+([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
或者为了使它更容易阅读...
$match_href = '|((https?://)?([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
$match_hash = '|\B#([\d\w_]+)|i';
$match_plus = '|\B\+([\d\w_]+)|i';
$replace_url = '<a href="$1">$3</a>';
$replace_tag = '<a href="http://url.com/pluslink/$1">$0</a>';
$status_text = preg_replace($match_href, $replace_url, $status_text);
$status_text = preg_replace($match_hash, $replace_tag, $status_text);
$status_text = preg_replace($match_plus, $replace_tag, $status_text);
用法示例
输入:(文本从DB - > $ STATUS_TEXT)
<!-- language-all: lang-none -->
Hi, this is an example. This is a url http://stackoverflow.com/
and this is a hash reference that we want to link to an internal
post #AwesomePost123 and this one is a plus reference we want to
link to an internal post +AwesomePost123 and finally an example
of a url without the http protocol www.stackoverflow.com
输出:
<!-- language-all: lang-none -->
Hi, this is an example. This is a url <a href="http://stackoverflow.com/">stackoverflow.com</a>
and this is a hash reference that we want to link to an internal
post <a href="http://url.com/pluslink/AwesomePost123">#AwesomePost123</a> and this one is a plus reference we want to
link to an internal post <a href="http://url.com/pluslink/AwesomePost123">+AwesomePost123</a> and finally an example
of a url without the http protocol <a href="www.stackoverflow.com">www.stackoverflow.com</a>
你可以给你正在使用的文本的例子吗? – mason81
@ mason81当然,它的一个简单的文本行,例如: hello http:// hello。 com(在这里避免自动链接的空格) – mobile
当你说你想要“不显示完整的URL,只是主要的域名”你指的是链接的href或链接的标签? – mason81