有时我们希望用户提交的连接地址自动给这个连接加上a标签了,这个其实就是给文本url地址加超级链接了,下面整理了几个例子希望对各位会带来帮助.
PHP实例代码如下:
$string = "这是php提取文本内容字符串中的网址,连接为http://www.Cuoxin.com 站点";
//连接后需要有个空格或回车。
$string = eregi_replace("http://([^ ,\r\n]*)","<a href=\\0 target=_blank>\\0</a>",$string);
$string = eregi_replace("ftp://([^ ,\r\n]*)","<a href=\\0 target=_blank>\\0</a>",$string);
例子1 代码如下:
<?php
$str='这样的http://www.Cuoxin.com/ play/?id=1010&p=1_0-1试试!';
$pattern='/(http:\/\/|https:\/\/|ftp:\/\/)([\w:\/\.\?=&-_]+)/is';
$str=preg_replace($pattern, '<a href=\1\2>\2</a>', $str);
echo $str;
?>
例子2 代码如下:
<?php
function autolink($foo)
{
$foo = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_/+.~#?&//=]+)', '<a href="/1" mce_href="/1" target=_blank rel=nofollow>/1</a>', $foo);
if( strpos($foo, "http") === FALSE ){
$foo = eregi_replace('(www.[-a-zA-Z0-9@:%_/+.~#?&//=]+)', '<a href="http:///1" mce_href="http:///1" target=_blank rel=nofollow >/1</a>', $foo);
}else{
$foo = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_/+.~#?&//=]+)', '/1<a href="http:///2" mce_href="http:///2" target=_blank rel=nofollow >/2</a>', $foo);
}
return $foo;
}
?>
大型站长资讯类网站! https://www.0818zz.com