How do I make a URL text a link?

Posted by jpjp on Stack Overflow See other posts from Stack Overflow or by jpjp
Published on 2010-05-29T15:12:17Z Indexed on 2010/05/29 15:22 UTC
Read the original article Hit count: 163

Filed under:

Say on Facebook or Twitter, when I type "www.google.com" and submit it, it becomes a link. How do I code this in PHP? Do I use regular expressions to get where the www starts and the .com ends?

Is this how they do it?

<?PHP 
//some regular expression to get www and .com part
$link="<a href='$url'>$url</a>";
echo $link;
?>

How do I write a regular expression to get the "www" and ".com" part?

And for twitter's @obama, obama would become a link to obama's site. What regular expression do they use to get the text after the @ and before the space?

© Stack Overflow or respective owner

Related posts about php