PHP Link all URL’s in a string…

Wanted all the URL’s and email addresses in a string to become actual HTML link when echoed, for our notepad software (link redacted).

After a while of searching I came across this…..

function makeLink($string){
//make sure there is an http:// on all URLs
$string = preg_replace("/([^w/])(www.[a-z0-9-]+.[a-z0-9-]+)/i", "$1http://$2",$string);
//make all URLs links
$string = preg_replace("/([w]+://[w-?&;#~=./@]+[w/])/i","<a target="_blank" href="$1">$1</a>",$string);
//make all emails hot links
$string = preg_replace("/([w-?&;#~=./]+@([?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?))/i","<a href="mailto:$1">$1</a>",$string);
return $string;

Makes all emails, mailto: and all links as links! Happy Days!

PHP Link all URL’s in a string…

Leave a Reply

Your email address will not be published. Required fields are marked *