PHP url 到数组

发布于 2024-12-14 23:30:49 字数 460 浏览 0 评论 0原文

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

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

如何在 PHP 中将此 URL 转换为数组,这样我将拥有一个像这样的数组

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

坦然微笑 2024-12-21 23:30:49

PHP 有一个本机函数,即 parse_str 函数。您可以像这样使用它:

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

这将满足您的需要。

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

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

This will do just what you need.

浅忆 2024-12-21 23:30:49

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

<?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

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

ま昔日黯然 2024-12-21 23:30:49

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

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.

仙气飘飘 2024-12-21 23:30:49

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

试试这个:

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

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

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

Try this:

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

$url_strings is an array containing all the URLs you want to parse in string format.
$urls_parsed is the resulting array containing the URLs parsed in the format you requested.

夏の忆 2024-12-21 23:30:49

使用像这样的 parse_url 函数,

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

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

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

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

Use parse_url function like

<?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.:)

水晶透心 2024-12-21 23:30:49

你可以这样做:

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);

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文