PHP url 到数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8067075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:53:16  来源:igfitidea点击:

PHP url to array

phparraysurl

提问by eric

I have these urls from a web log:

我有来自网络日志的这些网址:

http://www.domain.com/page?s=194&client=151678&m=a4a&v=1&g=54
http://www.domain.com/page?s=51&client=617171&m=b4z&v=8&g=97

How can I convert this URL in an array in PHPso i will have an array like this

如何在 PHP 中的数组中转换此 URL 所以我将有一个这样的数组

array(
 'page' => array(
   's' => 194,
   'client' => 151678
   'm' => 'a4a',
   'v' => 1,
   'g' => 54
 )
 etc..
)

then later I can loop to that array to find duplicates or validate the information correctly.

然后我可以循环到该数组以查找重复项或正确验证信息。

回答by Oldskool

PHP has a native function for that, the parse_strfunction. You can use it like:

PHP 有一个本地函数,parse_str函数。你可以像这样使用它:

parse_str($_SERVER['QUERY_STRING'], $outputArray);

This will do just what you need.

这将满足您的需求。

回答by RobFos

There may be a better way to do this but this is what I came up with.

可能有更好的方法来做到这一点,但这是我想出的。

<?php

    $url = 'http://www.domain.com/page?s=194&client=151678&m=a4a&v=1&g=54';
    $remove_http = str_replace('http://', '', $url);
    $split_url = explode('?', $remove_http);
    $get_page_name = explode('/', $split_url[0]);
    $page_name = $get_page_name[1];

    $split_parameters = explode('&', $split_url[1]);

    for($i = 0; $i < count($split_parameters); $i++) {
        $final_split = explode('=', $split_parameters[$i]);
        $split_complete[$page_name][$final_split[0]] = $final_split[1];
    }

    var_dump($split_complete);

?>

http://codepad.org/xTsAks46

http://codepad.org/xTsAks46

回答by matzino

With parse_url()you can parse the URL to an associative array. In the result array you get amongst others the query part. Then you can use parse_str()for parsing the query part to your new array.

有了parse_url()你可以解析URL关联数组。在结果数组中,您可以获得查询部分。然后您可以 parse_str()用于将查询部分解析为您的新数组。

回答by Kostas Andrianopoulos

Assuming from your question that path will always be /page, the array containing the parsed URLs cannot have multiple pageindexes so you should use numeric indexes.

假设从您的问题 path 将始终是/page,包含解析的 URL 的数组不能有多个page索引,因此您应该使用数字索引。

Try this:

尝试这个:

$urls_parsed = array();
foreach ($url_strings as $url_string) {
    $url = parse_url($url_string);
    $urls_parsed[] = parse_str($url['query']);
}

$url_stringsis an array containing all the URLs you want to parse in string format. $urls_parsedis the resulting array containing the URLs parsed in the format you requested.

$url_strings是一个包含要以字符串格式解析的所有 URL 的数组。 $urls_parsed是包含以您请求的格式解析的 URL 的结果数组。

回答by kta

Use parse_url function like

使用 parse_url 函数,如

<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>

Which will give you the following array output

这将为您提供以下数组输出

Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)

And from the query you can easily split it as to (key,value) paira. Hope this helps.:)

从查询中,您可以轻松地将其拆分为(键,值)对。希望这可以帮助。:)

回答by Nicola Peluchetti

You could do:

你可以这样做:

function convertUrlToArray($url){
    $url = str_replace("http://www.domain.com/page?", "", $url);
    $urlExploded =  explode("&", $url);
    $return = array();
    foreach ($urlExploded as $param){
        $explodedPar = explode("=", $param);
         $return[$explodedPar[0]] = $explodedPar[1];
    }
    return $return;

}

$url1 = convertUrlToArray("http://www.domain.com/page?s=51&client=617171&m=b4z&v=8&g=97");
var_dump($url1);

example here http://codepad.org/KIDKPzaU

这里的例子http://codepad.org/KIDKPzaU