preload

How To Validate A URL in PHP Tutorial

This tutorial will teach you how to build a function that can be used by PHP developers to validate a URL data-type.

All you need to do is pass in variable data to the function and it will return true if it is a proper URL or false if it is not. Use this to easily check is a variable is a properly formed URL. It uses a regular expression (regex) to validate the URL.

The URL Data Validation Function


function validateURL($URL) {
return (bool)preg_match("/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i", $URL);
}

How To Call The Function


if (validateURL('http://www.krio.me') {

// If URL is valid, do something here

}

Use this in all of your PHP scripts to ensure that input provided by users for URL’s is of a valid data type. Need a website developer to help you out? Contact me if you need some help. I’m a web developer and designer local to Miami, Florida.

Trackbacks

  1. [...] is the original post: How To Validate A URL in PHP Tutorial Share and
  2. [...] here: How To Validate A URL in PHP Tutorial Share and [...]

Leave a Reply

* Required
** Your Email is never shared