首页 > 解决方案 > 在php中添加文本链接的链接,但前提是链接在我自己的网站中

问题描述

我知道这里有很多帖子可以将文本链接转换为可点击的链接。但我的情况有点不同。

我只想转换我网站上的链接。不是其他网站。

所以我尝试了:

$text = "The text you want to filter goes here. http://example.com/site/site";


echo preg_replace('$(\s|^)(https?://[example.com]+)(?![^<>]*>)$i', ' <a href="$2" target="_blank">$2</a> ', $text." ");

它可以工作,但问题是如果 url 包含类似:www.example.com/example/article/01/3232/ 如果只会获取 example.com。

任何想法如何让它按我的意愿工作?

标签: phpregex

解决方案


推荐阅读