Zend - The PHP Company




HTML

Add Code


autoLink  

Type: application
Added by: goten
Entered: 13/04/2001
Last modified: 04/12/2000
Rating: - (fewer than 3 votes)
Views: 7941
Auto link URL and Email address in a document. And it will not mess up &lta href> tag like many other auto link script did. Also, it only use two preg_replace statement for FAST translation.
(1.2 version fix the HTML A tag not giving out the correct URL)
Restriction: Any URL begin with " or "> will not be linked.


<?php
function autoLink($str) {
// link all urls and email 

    
$str2 preg_replace("/(?<!<a href=")(?<!")(?<!">)((http|https|ftp)://[w?=&./-]+)/","<a href="\1">\1</a>",$str);
    
$str2 preg_replace("/((?<!<a href="mailto:)(?<!">)(?<=(>|s))[w_-]+@[w_.-]+[w]+)/","<a href="mailto:\1">\1</a>",$str2);    return $str2;
}
?>


Usage Example


$str = "Testing regular URL <b>http://www.yahoo.com/test/test.html?sss=aa&aa=dd</b>.<br>  Testing email <b>test@test.test.com</b><br> Testing email A tag <font size=+1><a href="mailto:sss@sss.eee.com">woo@woo.com</a><br>Now I am testing HTML A tag <a href="http://www.aaa.com/sss/aee.html" target="_base"><font color="red">http://www.asss.com/aa/aaa</font></a></font>";

echo autoLink($str);



Rate This Script





Search



This Category All Categories