php – 将文本链接替换为preg_replace的链接
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 将文本链接替换为preg_replace的链接,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4003字,纯文字阅读大概需要6分钟。
内容图文
![php – 将文本链接替换为preg_replace的链接](/upload/InfoBanner/zyjiaocheng/748/833b38eeb58d4a2ea87fc685ca58f66d.jpg)
我似乎有一个问题,用发布的网站链接替换文本链接,它没有链接.
代码:
$status_text = preg_replace('#(\A|[^=\]\'"a-zA-Z0-9])(http[s]?://(.+?)/[^()<>\s]+)#i', '\\1<a href="\\2">\\3</a>', $status_text);
echo $status_text;
$status_text是从名为contents的MySQL字段中提取的,包含其他文本,但我只是想链接链接.此外,我还希望它不显示完整的URL,只显示主域.
更新:我们在同一页面上有另外两个preg_replaces,在它们前面找到链接到站点区域的东西,它们目前正在工作,不需要与上面的冲突:
$status_text = preg_replace("/#([a-z_0-9]+)/i", "<a href=\"http://url.com/hashlink/$1\">$0</a>", $status_text);
$status_text = preg_replace("/\+([a-z_0-9]+)/i", "<a href=\"http://url.com/pluslink/$1\">$0</a>", $status_text);
解决方法:
在这里,试试这个:
$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;
或者让它更容易阅读:
$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中的新信息而更新 –
你的hashtag正则表达式没有考虑URL中的哈希值,所以让我们解决这个问题……
此外,您应该在匹配hash-tags或plus-tags之前匹配URL,否则您将搞乱为hash-tags和plus-tags创建的链接
$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);
再次编辑 – 添加可能有用的网址 –
你可以在这里测试正则表达式:http://gskinner.com/RegExr/
另一个编辑
根据评论/问题,如果您想要考虑缺少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 https://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="https://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>
内容总结
以上是互联网集市为您收集整理的php – 将文本链接替换为preg_replace的链接全部内容,希望文章能够帮你解决php – 将文本链接替换为preg_replace的链接所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。