PHP parser url - domain returned as path when protocol prefix is ​​missing

I am trying to parse a URL in PHP where the input can be any of the following:

The code:

$info = parse_url('http://www.domainname.com/');
print_r($info);

$info = parse_url('www.domain.com');
print_r($info);

$info = parse_url('/test/');
print_r($info);

$info = parse_url('test.php');
print_r($info);

Return:

Array
(
    [scheme] => http
    [host] => www.domainname.com
    [path] => /
)
Array
(
    [path] => www.domain.com
)
Array
(
    [path] => /test/
)
Array
(
    [path] => test.php
)

The problem you see is the second example when a domain is returned as a path.

+3
source share
2 answers

This gives the correct results, but the file should start with a slash:

parse('http://www.domainname.com/');
parse('www.domain.com');
parse('/test/');
parse("/file.php");

function parse($url){
    if(strpos($url,"://")===false && substr($url,0,1)!="/") $url = "http://".$url;
    $info = parse_url($url);
    if($info)
    print_r($info);
}

and the result:

Array
(
    [scheme] => http
    [host] => www.domainname.com
    [path] => /
)
Array
(
    [scheme] => http
    [host] => www.domain.com
)
Array
(
    [path] => /test/
)
Array
(
    [path] => /file.php
)
+7
source

To handle the URL so that it preserves the URL without a scheme, while allowing you to identify the domain, use the following code.

if (!preg_match('/^([a-z][a-z0-9\-\.\+]*:)|(\/)/', $url)) {
    $url = '//' . $url;
}

So, this will apply "//" to the beginning of the URL only if the URL does not have a valid pattern and does not start with "/".

:

() , ":" , , "//", . , URL , , , "://".

  • [scheme]:[path//path]
  • //[domain][/path]
  • [scheme]://[domain][/path]
  • [/path]
  • [path]

PHP URL- parse_url(), , .

: alpha *( alpha | digit | "+" | "-" | "." )

0

All Articles