Over a million developers have joined DZone.

Make Anchors From Urls And Email Addresses

DZone's Guide to

Make Anchors From Urls And Email Addresses

Free Resource
This little PHP function will find urls and email addresses in a block of text and turn them into hyperlinks and mailto: anchors respectively.

function makeLinks($sourceText) {
  $destText = preg_replace( "/([_a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+)(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})/", '\\0',$sourceText);
  $destText = preg_replace_callback('/\bhttp[^\s]+/',create_function('$matches', 'return "" . preg_replace("#(\.|/)#", "­$1", $matches[0]) . "";'),$destText);
  return $destText;

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}