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
Beispiel #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'));
 }
Beispiel #2
0
 /**
  * Create an HTML link to a given URL or contenttype/slug pair.
  *
  * @param string $location
  * @param string $label
  *
  * @return string
  */
 public function link($location, $label = '[link]')
 {
     if ((string) $location === '') {
         return '';
     }
     if (Html::isURL($location)) {
         $location = Html::addScheme($location);
     } elseif ($record = $this->app['storage']->getContent($location)) {
         $location = $record->link();
     }
     return sprintf('<a href="%s">%s</a>', $location, $label);
 }