开发者

How do I parse a URL in PHP? [duplicate]

This question already has answers here: Closed 10 years ago. 开发者_运维技巧

Possible Duplicate:

Parsing Domain From URL In PHP

how do i get

http://localhost/

from

http://localhost/something/

using php

I tried

    $base  = strtok($url, '/');


You can use parse_url to get down to the hostname.

e.g.:

$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
)
*/


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


Check out parse_url() - http://php.net/parse_url

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜