Best Method to Validate URL

Permalink 1 user found helpful
I am working on a block that requires a URL to be entered. I have tried searching, but haven't found anything that works. The block form has this:
<?php  echo $form->url('iframeurl', $iframeurl?$iframeurl:"");?>

In the block controller, I have tried these:
$validurl = '%^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@|\d{1,3}(?:\.\d{1,3}){3}|(?:(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)(?:\.(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)*(?:\.[a-z\x{00a1}-\x{ffff}]{2,6}))(?::\d+)?(?:[^\s]*)?$%iu';
      if(!preg_match($validurl,($args[$iframeurl]))) {
         $e->add(t("Iframe URL is invalid."));
      }

and
if(!filter_var(($args[$iframeurl]), FILTER_VALIDATE_URL) === false) {
         $e->add(t("IFrame URL is invalid."));
      }

Neither of those work.

What is the best way to validate URLs?

PineCreativeLabs
 
whollands replied on at Permalink Reply
whollands
Here is one I modified off:
http://www.w3schools.com/php/php_form_url_email.asp...

$website = "http://example.com";
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) 
{
  // stuff to do if URL is invalid
}

Hopefully this should work for you - I use this in most of my web applications
aditya12 replied on at Permalink Reply
<a href="https://www.welookups.com/php/php_form_url_email.html">https://www.welookups.com/php/php_form_url_email.html</a>