Regular expressions can be very confusing at times, but they are extremely powerful to use when coding. It makes validation of user input a lot easier.

The example below will show you have to use a simple regular expression to check whether a domain name is valid or not.

  $url = "http://www.phpdevelopment.co.za/";
  if (preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $url)) {
    echo "Your url is fine.";
  } else {
    echo "Your url is not fine.";
  }