开发者

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

开发者 https://www.devze.com 2023-02-24 00:55 出处:网络
This question already has answers here: Closed 10 years ago. 开发者_运维技巧 Possible Duplicate: Parsing Domain From URL In PHP
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

精彩评论

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