Example: | site.ru/page.php | /page.php | site.ru/page.php |
---|---|---|---|
scheme | http | ||
host | site.ru | site.ru | |
path | page.php | page.php | page.php |
Zend\Uri\Http
class should help us with this. It has the methods we need: parse($uri)
, getHost()
, getPath()
, etc.site.ru/page.php
” (without “ http://
”), getHost()
returns an empty string, and getPath()
returns “ site.ru/page.php
”. public function myParse($url){ $Http = new Http($url); if($Http->isValidRelative()){ // url $path = $Http->getPath(); // path «/» — «» // if( $path{0} !== '/' ){ // ... $absoluteUrl = '//'.urldecode($Http->toString()); $absoluteHttp = new Http($absoluteUrl); // (1) $Hostname = new Hostname(array('allow'=>Hostname::ALLOW_DNS, 'useTldCheck'=>false)); $decode = true; // ... (2) if ($Hostname->isValid($absoluteHttp->getHost($decode))) { // , «» $Http = $absoluteHttp; } } } return $Http; }
Zend\Validator\Hostname
to check the presence of the first-level domain of the link in the $validIdns
getHost()
method the $decode = true;
in order to decode the host. The getHost()
method of the Zend\Uri\Http
class does not imply any parameters and does not decode anything! Why then and how does it work?! .. Read below.Zend\Uri\Http
. namespace Application\Other; use Zend\Uri\Http as ZendHttp; use Application\Model\IdnaConvert; class Http extends ZendHttp { public function setHost($host){ if($host){ $idn = new IdnaConvert(); $host = $idn->encode($host); } return parent::setHost($host); } public function getHost($decode=false) { if($decode && $this->host){ $idn = new IdnaConvert(); return $idn->decode($this->host); } return parent::getHost(); } }
myParse()
should use the extended class Http
, which, when parsing a URL, will be able to encode RF domains; and when calling the getHost($decode)
method, we will be able to return the Punycode representation or decoded representation, depending on the parameter passed to the method.Source: https://habr.com/ru/post/198614/
All Articles