WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

May 20 2012

A REAL Regular Expression (Regex) for URLs using preg_match()

Today I wanted to find a regex expression that would validate any URL. For example, I wanted it to be able to validate the follow urls where the .com could be any extension:

  • http://domain.com, http://www.domain.com, http://subdomain.domain.com
  • https://domain.com, https://www.domain.com, https://subdomain.domain.com
  • www.domain.com
  • subdomain.domain.com
  • domain.com

Since I too wasted a ton of time looking for this I wanted to do a re-blog/re-post on this for me because I am not sure if I will find all those posts again.

I have slightly (and very slightly) modified splattermania's post.

As I wasted lots of time finding a REAL regex for URLs and resulted in building it on my own, I now have found one, that seems to work for all kinds of urls:

[php]<?php
$regex = "((https?|ftp)://)?"; // SCHEME
// My Version: $regex = "((https?|ftps?)://)?"; // SCHEME
$regex .= "([a-z0-9+!*(),;?&=$_.-]+(:[a-z0-9+!*(),;?&=$_.-]+)?@)?"; // User and Pass
$regex .= "([a-z0-9-.]*).([a-z]{2,3})"; // Host or IP
$regex .= "(:[0-9]{2,5})?"; // Port
$regex .= "(/([a-z0-9+$_-].?)+)*/?"; // Path
$regex .= "(?[a-z+&$_.-][a-z0-9;:@&%=+/$_.-]*)?"; // GET Query
$regex .= "(#[a-z_.-][a-z0-9+$_.-]*)?"; // Anchor
[/php]

Then, the correct way to check against the regex ist as follows:

[php]<?php
if( preg_match( "/^$regex$/", $url ) )
return true;
[/php]

splattermania's post is priceless and belongs on php.net, and it shows the value in checking php.net before Googling or in the midst of Googling for information about PHP.

You can test the expression with the Regular Expression (Regex) Test Tool by selecting preg_match() & entering the following:
[code gutter="false"]/^((https?|ftps?)://)?([a-z0-9+!*(),;?&=$_.-]+(:[a-z0-9+!*(),;?&=$_.-]+)?@)?([a-z0-9-.]*).([a-z]{2,3})(:[0-9]{2,5})?(/([a-z0-9+$_-].?)+)*/?(?[a-z+&$_.-][a-z0-9;:@&%=+/$_.-]*)?(#[a-z_.-][a-z0-9+$_.-]*)?/[/code]

So as a practical use in one line:
[php]<?php
if ( preg_match( '/^((https?|ftp)://)?([a-z0-9+!*(),;?&=$_.-]+(:[a-z0-9+!*(),;?&=$_.-]+)?@)?([a-z0-9-.]*).([a-z]{2,3})(:[0-9]{2,5})?(/([a-z0-9+$_-].?)+)*/?(?[a-z+&$_.-][a-z0-9;:@&%=+/$_.-]*)?(#[a-z_.-][a-z0-9+$_.-]*)?/', $url ) )
// do something
[/php]

Now, later you may need to add http:// to the front of a "validated" URL. Here's what I used:
[php]<?php
if ( isset( $url ) && ! preg_match( '~^(?:f|ht)tps?://~i', $url ) )
$url = 'http://' . $url;
[/php]

Or as a function:
[php]<?php
wps_url_fix('test.com' ); // http://test.com
wps_url_fix('www.test.com' ); // http://www.test.com
wps_url_fix('test.com' ); // http://test.com
wps_url_fix('ftp://test.com' ); // ftp://test.com
wps_url_fix('https://test.com' ); // https://test.com
wps_url_fix('http://test.com' ); // http://test.com
wps_url_fix('test' ); // http://test

/**
* Fixes URL, adds http:// if missing
*
* @param string $url String containing URL with(out) prefix
*/
function wps_url_fix( $url ) {
if ( isset( $url ) && ! preg_match( '~^(?:f|ht)tps?://~i', $url ) )
$url = 'http://' . $url;
return $url;
}
[/php]

Written by Travis Smith · Categorized: WordPress

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in