Posted By

tylerhall on 03/21/07


Tagged

regex url match


Versions (?)


Advertising

Website Promotion DIRECTORY is a crucial factor for all websites that need to gain better organic search engine rankings and increase website traffic.
Submitting your website as part of your Web Promotion strategy to our SEO friendly and high traffic Business Directory for review is an excellent way to gain a valuable backlink and increase your websites visibility online.

Submit Site


Who likes this?

25 people have marked this snippet as a favorite

bbebop
eunjoo1984
basicmagic
mafro
mjlassila
jbo
benrasmusen
gardnern
skywalker
pixelhandler
resurge
wirjo
derekpcollins
irishsk
atma
Caimin
nb109
meggesje
jim0thy
staydecent
goozak
tannerc
joshOiknine
ericschweichler
redstorm


Regex (regular expression) to match a URL


Published in: PHP 






This regular expression matches a URL.

Expand | Embed | Plain Text
  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 

Comments

RSS Icon Subscribe to comments
Posted By: hathrill on August 20, 2009

Exactly what I needed. I heart you.

Posted By: jpo on September 1, 2009

Perfect. Thank you!

Posted By: cjastram on October 21, 2009

Perfect for copy-and-paste. Very nice regex, thank you! -cej102937

Posted By: goozak on November 10, 2009

Great regex, thanks ! Small thing : the '-' (dash) is missing - URL like this fails (http://web5.uottawa.ca/admingov/reglements-methodes.html)

Posted By: goozak on November 10, 2009

Great regex, thanks ! Small thing : the '-' (dash) is missing - URL like this fails (http://web5.uottawa.ca/admingov/reglements-methodes.html)

Posted By: inhahe on November 28, 2009

it's also missing left parenthesis, right parenthesis, and comma.

Posted By: tannerc on December 18, 2009

Perfect! I spent the better part of this morning looking for a great URL matching snippet and finally found it here. Thank you!

Posted By: joshOiknine on January 14, 2010

Took me a few min to track this down on Google but it was exactly what I was looking for. Thank You.

You need to login to post a comment.

Download royalty free graphics