Bolt\Helpers\Html::isURL PHP Method

isURL() public static method

Check if a given string looks like it could be a URL, with or without the protocol.
See also: https://mathiasbynens.be/demo/url-regex
public static isURL ( string $str ) : boolean
$str string
return boolean
    public static function isURL($str)
    {
        $pattern = '~^(?:\\b[a-z\\d.-]+://[^<>\\s]+|\\b(?:(?:(?:[^\\s!@#$%^&*()_=+[\\]{}\\|;:\'",.<>/?]+)\\.)+(?:ac|ad|aero|ae|af|ag|ai|al|am|an|ao|aq|arpa|ar|asia|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|biz|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|cat|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|coop|com|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|info|int|in|io|iq|ir|is|it|je|jm|jobs|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mobi|mo|mp|mq|mr|ms|mt|museum|mu|mv|mw|mx|my|mz|name|na|nc|net|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pro|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|travel|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|xn--0zwm56d|xn--11b5bs3a9aj6g|xn--80akhbyknj4f|xn--9t4b11yi5a|xn--deba0ad|xn--g6w251d|xn--hgbk6aj7f53bba|xn--hlcj6aya9esc7a|xn--jxalpdlp|xn--kgbechtv|xn--zckzah|ye|yt|yu|za|zm|zw)|(?:(?:[0-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.){3}(?:[0-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5]))(?:[;/][^#?<>\\s]*)?(?:\\?[^#<>\\s]*)?(?:#[^<>\\s]*)?(?!\\w))$~iS';
        // Special case that isn't caught by this regex: 'http://' or 'https://' without a domain.
        if (preg_match('~^https?://$~i', $str)) {
            return false;
        }
        return (bool) preg_match($pattern, $str . '/');
    }

Usage Example

Example #1
0
 public function testIsURL()
 {
     $this->assertTrue(Html::isURL('example.org'));
     $this->assertTrue(Html::isURL('example.org/'));
     $this->assertTrue(Html::isURL('www.example.org'));
     $this->assertTrue(Html::isURL('https://example.org'));
     $this->assertTrue(Html::isURL('https://example.org/'));
     $this->assertTrue(Html::isURL('http://foo.com/blah_blah'));
     $this->assertTrue(Html::isURL('http://foo.com/blah_blah/'));
     $this->assertTrue(Html::isURL('http://foo.com/blah_blah_(wikipedia)'));
     $this->assertTrue(Html::isURL('http://foo.com/blah_blah_(wikipedia)_(again)'));
     $this->assertTrue(Html::isURL('http://www.example.com/wpstyle/?p=364'));
     $this->assertTrue(Html::isURL('https://www.example.com/foo/?bar=baz&inga=42&quux'));
     $this->assertTrue(Html::isURL('http://142.42.1.1/'));
     $this->assertTrue(Html::isURL('http://142.42.1.1:8080/'));
     $this->assertTrue(Html::isURL('http://j.mp'));
     $this->assertTrue(Html::isURL('ftp://foo.bar/baz'));
     $this->assertTrue(Html::isURL('http://1337.net'));
     $this->assertTrue(Html::isURL('http://a.b-c.de'));
     $this->assertTrue(Html::isURL('http://223.255.255.254'));
     $this->assertFalse(Html::isURL('http://'));
     $this->assertFalse(Html::isURL('https://'));
     $this->assertFalse(Html::isURL('//'));
     $this->assertFalse(Html::isURL('//a'));
     $this->assertFalse(Html::isURL('http:/example.org'));
     $this->assertFalse(Html::isURL('http:example.org'));
 }
All Usage Examples Of Bolt\Helpers\Html::isURL