有時(shí)我們希望用戶提交的連接地址自動(dòng)給這個(gè)連接加上a標(biāo)簽了,這個(gè)其實(shí)就是給文本url地址加超級(jí)鏈接了,下面整理了幾個(gè)例子希望對(duì)各位會(huì)帶來(lái)幫助.
PHP實(shí)例代碼如下:
- $string = "這是php提取文本內(nèi)容字符串中的網(wǎng)址,連接為http://m.survivalescaperooms.com 站點(diǎn)";
- //連接后需要有個(gè)空格或回車。
- $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://m.survivalescaperooms.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;
- }
- ?>
新聞熱點(diǎn)
疑難解答