We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Ballyhoo


Posted By

tylerhall on 03/21/07


Tagged

regex url match


Versions (?)


Who likes this?

9 people have marked this snippet as a favorite

bbebop
eunjoo1984
basicmagic
mafro
mjlassila
jbo
benrasmusen
gardnern
skywalker


Regex (regular expression) to match a URL


Published in: PHP 


This regular expression matches a URL.


  1. https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?
  2.  
  3. PHP Example: Automatically link URL's inside text.
  4.  
  5. $text = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $text);

Report this snippet 

You need to login to post a comment.