gpt4 book ai didi

php - 字符串中的自动链接 URL

转载 作者:行者123 更新时间:2023-11-28 05:08:08 30 4
gpt4 key购买 nike

我有一个正常的消息输出 $msg。我想让它成为链接,如果它是链接的话。 (包含 http://或 www.)那么它应该是 <a href="http://google.com" target="_blank">http://google.com</a>

我已经从消息中剥离了 html

$msg = htmlspecialchars(strip_tags($show["status"]), ENT_QUOTES, 'utf-8')

这怎么能做到,在很多地方都看到了。

最佳答案

我遇到了与@SublymeRick 相同的问题(在第一个点后停止,请参阅 Auto-link URLs in a string)。

https://stackoverflow.com/a/8218223/593957 得到一点启发我改成了

$msg = preg_replace('/((http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?)/', '<a href="\1">\1</a>', $msg);

关于php - 字符串中的自动链接 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4144837/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com