PHP的HTML创建文本链接 [英] php html create link from text
问题描述
<?php $ b我发现了一个例程来创建一个html链接$ b函数makelink($ text)
{
return preg_replace('/(http \:\ / \ / [a-zA-Z0-9_\-\。] *? )/ i','< a href =$ 1> $ 1< / a>',$ text。);
}
//作品
echo makelink(hello how http://www.guruk.com);
//不工作
echo makelink(hello how http://www.guruk.com/test.php);
?>
这个例子,它的工作原理只能找到一个域名,而不是当那个链接中有一个页面或子目录。
你能否提供一个解决方案,与页面和subdirs?
thx
chris
?=&
这些字符适用于带有查询字符串的网址。请注意,我将分隔符从 /
更改为!
,因为表达式中有很多斜杠。另请注意,如果您处于不区分大小写模式,则不需要 AZ
。
return preg_replace('!(http:// [a-z0-9_./?=&-] +)!i','< a href =$ 1> $ 1< a>',$ text。);
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?
thx chris
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创建文本链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!