如何在PHP中解析URL?

Mat*_*iby 0 php

可能重复:
在PHP中从URL解析域

如何得到

http://localhost/
Run Code Online (Sandbox Code Playgroud)

http://localhost/something/
Run Code Online (Sandbox Code Playgroud)

用php

我试过了

    $base  = strtok($url, '/');
Run Code Online (Sandbox Code Playgroud)

hal*_*dan 5

您可以使用parse_url来获取主机名.

例如:

$url = "http://localhost/path/to/?here=there";
$data = parse_url($url);
var_dump($data);

/*
Array
(
    [scheme] => http
    [host] => localhost
    [path] => /path/to/
    [query] => here=there
)
*/
Run Code Online (Sandbox Code Playgroud)


gma*_*add 5

$url = 'http://localhost/something/';
$parsedurl  = parse_url($url);
echo $parsedurl['scheme'].'://'.$parsedurl['host'];
Run Code Online (Sandbox Code Playgroud)