Improvement of URL interpretation with regex


/ Published in: PHP
Save to your folder(s)

A source to help you interpreting URLs in plain text as a HTML link. It's an improvement of what was proposed here : http://snipplr.com/view/2371/regex-regular-expression-to-match-a-url/


Copy this code and paste it in your HTML
  1. // Returns the text given as parameter, with contained URLs interpreted as HTML links
  2.  
  3. function link($text){
  4. return preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '<a href="$1">$1</a>', $text);
  5. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.