php html create link from text(php html 从文本创建链接)
问题描述
我找到了在文本中找到链接时创建 html 链接的例程
I found a routine to create a html link when a link is found in a text
<?php
function makelink($text)
{
return preg_replace('/(http://[a-zA-Z0-9_-.]*?) /i', '<a href="$1">$1</a> ', $text." ");
}
// works
echo makelink ("hello how http://www.guruk.com ");
// dont work
echo makelink ("hello how http://www.guruk.com/test.php ");
?>
如您在示例中所见,它仅适用于域,而不适用于该链接中存在页面或子目录的情况.
as you see in the example, it works find with a domain only, not when there is a page or subdirectory is within that link.
您能否为该功能提供一个解决方案,使其也适用于页面和子目录?
Can you provide a solution for that function to work also with pages and subdirs?
谢谢克里斯
推荐答案
字符 ?=&
用于带有查询字符串的 url.请注意,我将分隔符从 /
更改为 !
因为您的表达式中有很多斜线.另请注意,如果您处于不区分大小写的模式,则不需要 A-Z
.
The characters ?=&
are for urls with query strings. Notice that I changed the separator from /
to !
because there a lot of slashes in your expression. Also note that you don't need A-Z
if you are in case-insensitive mode.
return preg_replace('!(http://[a-z0-9_./?=&-]+)!i', '<a href="$1">$1</a> ', $text." ");
这篇关于php html 从文本创建链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:php html 从文本创建链接


基础教程推荐
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01