I found this great piece of code for validating URL's. I've been trying to get it to work with preg_match() since eregi() is deprecated with with PHP 5.3:

// SCHEME
$urlregex = "^(https?|ftp)://";

// USER AND PASS (optional)
$urlregex .= "([a-z0-9+!*(),;?&=$_.-]+(:[a-z0-9+!*(),;?&=$_.-]+)?@)?";

// HOSTNAME OR IP
$urlregex .= "[a-z0-9+$_-]+(.[a-z0-9+$_-]+)*"; // http://x = allowed (ex. http://localhost, http://routerlogin)
//$urlregex .= "[a-z0-9+$_-]+(.[a-z0-9+$_-]+)+"; // http://x.x = minimum
//$urlregex .= "([a-z0-9+$_-]+.)*[a-z0-9+$_-]{2,3}"; // http://x.xx(x) = minimum
//use only one of the above

// PORT (optional)
$urlregex .= "(:[0-9]{2,5})?";
// PATH (optional)
$urlregex .= "(/([a-z0-9+$_-].?)+)*/?";
// GET Query (optional)
$urlregex .= "(?[a-z+&$_.-][a-z0-9;:@/&%=+$_.-]*)?";
// ANCHOR (optional)
$urlregex .= "(#[a-z_.-][a-z0-9+$_.-]*)?$";

// check
if (eregi($urlregex, $url)) {echo "good";} else {echo "bad";}

The problem seems to be that this is POSIX and for preg it needs to be PCRE. I'm having quite a time with it. Anyone know how to fix it?

Right. Many things need to be escaped. Here's my attempt:

/* Make sure it's a properly formatted URL. */
  // Scheme
  $urlregex = '^(https?|s?ftp\:\/\/)|(mailto\:)';
  // User and password (optional)
  $urlregex .= '([a-z0-9\+!\*\(\)\,\;\?&=\$_\.\-]+(\:[a-z0-9\+!\*\(\)\,\;\?&=\$_\.\-]+)?@)?';
  // Hostname or IP
    // http://x = allowed (ex. http://localhost, http://routerlogin)
    //$urlregex .= '[a-z0-9\+\$_\-]+(\.[a-z0-9\+\$_\-]+)*';
    // http://x.x = minimum
    $urlregex .= "[a-z0-9\+\$_\-]+(\.[a-z0-9+\$_\-]+)+";
    // http://x.xx(x) = minimum
    //$urlregex .= "([a-z0-9\+\$_\-]+\.)*[a-z0-9\+\$_\-]{2,3}";
  //use only one of the above
  // Port (optional)
  $urlregex .= '(\:[0-9]{2,5})?';
  // Path (optional)
  //$urlregex .= '(\/([a-z0-9\+\$_\-]\.\?)+)*\/?';
  // GET Query (optional)
  $urlregex .= '(\?[a-z\+&\$_\.\-][a-z0-9\;\:@\/&%=\+\$_\.\-]*)?';
  // Anchor (optional)
  //$urlregex .= '(\#[a-z_\.\-][a-z0-9\+\$_\.\-]*)?$';
if(preg_match('/'.$urlregex.'/i', $link))
{
  $url='ok';
}

It seems to be working well. Thoughts on how I could make it better?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.