Over a million developers have joined DZone.

Make Anchors From Urls And Email Addresses

·
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;
}
Topics:

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

{{ parent.tldr }}

{{ parent.urlSource.name }}