有時我們希望用戶提交的連接地址自動給這個連接加上a標簽了,這個其實就是給文本url地址加超級鏈接了,下面整理了幾個例子希望對各位會帶來幫助.
PHP實例代碼如下:
- $string = "這是php提取文本內容字符串中的網址,連接為http://www.49028c.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.49028c.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;
- }
- ?>
新聞熱點
疑難解答