<p>自动匹配页面里的网址,包含http,ftp等,自动给网址加上链接</p><pre class="brush:php;toolbar:false">functiontext2links($str=&#39;&#39;){ if($str==&#39;&#39;or!preg_match(&#39;/(http|www\.|@)/i&#39;,$str)){return$str;} $lines=explode(&quot;\n&quot;,$str);$new_text=&#39;&#39;; while(list($k,$l)=each($lines)){ //replacelinks: $l=preg_replace(&quot;/([\t]|^)www\./i&quot;,&quot;\\1http://www.&quot;,$l); $l=preg_replace(&quot;/([\t]|^)ftp\./i&quot;,&quot;\\1ftp://ftp.&quot;,$l); $l=preg_replace(&quot;/(http:\/\/[^)\r\n!]+)/i&quot;, &quot;&lt;ahref=\&quot;\\1\&quot;&gt;\\1&lt;/a&gt;&quot;,$l); $l=preg_replace(&quot;/(https:\/\/[^)\r\n!]+)/i&quot;, &quot;&lt;ahref=\&quot;\\1\&quot;&gt;\\1&lt;/a&gt;&quot;,$l); $l=preg_replace(&quot;/(ftp:\/\/[^)\r\n!]+)/i&quot;, &quot;&lt;ahref=\&quot;\\1\&quot;&gt;\\1&lt;/a&gt;&quot;,$l); $l=preg_replace( &quot;/([-a-z0-9_]+(\.[_a-z0-9-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)+))/i&quot;, &quot;&lt;ahref=\&quot;mailto:\\1\&quot;&gt;\\1&lt;/a&gt;&quot;,$l); $new_text.=$l.&quot;\n&quot;; } return$new_text; } //使用范例: $text=&quot;Visitwww.jonasjohn.de:-)&quot;; printtext2links($text);</pre>
T:0.126560s,M:204.79 KB
返回顶部 留言