Grow your CSS skills. Land your dream job.

Last updated on:

URL Validation

$url = '';
$validation = filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED);

if ( $validation ) $output = 'proper URL';
else $output = 'wrong URL';

echo $output;


  1. Will
    Permalink to comment#

    $output = ‘proper UL';
    I think there is a typo in that line.

  2. Emilio CS
    Permalink to comment#

    I think it needs improvements. The code will pass the url “http://www.example” as valid url.

  3. filter_var is good one but I still have faith in good old preg_match() and the regular expressions.


  4. My regex for URL validation (validates GET variables and “#” after URLs too):

    preg_match("/^(https?:\/\/)([\da-z\.-]+)\.([a-z\.]{2,6})(\/([\da-z\.-]+))*\/?(([\w\.-]+)\.([\da-z]{2,6}))?((\#[\w\.-]+)|(\?([\da-z]+(=[\da-z]+)?)(&([\da-z]+(=[\da-z]+)?))*))?/i", $url);

Leave a Comment

Posting Code

  • Use Markdown, and it will escape the code for you, like `<div class="cool">`.
  • Use triple-backticks for blocks of code.
      <h1>multi-line block of code</h1>
      <span>be cool yo.</span>
  • Otherwise, escape your code, like <code>&lt;div class="cool"&gt;</code>. Markdown is just easier though.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".