在 PHP 中获取查询字符串不起作用

发布于 2024-12-07 01:28:16 字数 845 浏览 1 评论 0原文

我是 php 新手,我正在尝试遵循有关如何获取查询字符串的建议。我希望以可读的格式搜索单词。

现在,字符串来自数据库,它们之前是使用 Asp.Net 中的请求对象的referer 属性收集的。但据我所知,这应该没有什么区别。

所以我尝试了这个:

function getQuery($url)
{
        $processed_url = parse_url( $url );

        $query_string = $processed_url['query'];
        if($url != '')
        {
            return $query_string;
        }
        else
        {
            return $url;
        }
}

所以我尝试了一个变体,它也应该提取查询字符串:

    $query = parse_url($url, 6);

    return $query;

嗯,这样的工作。它给了我一个查询字符串部分,但包括“q=”和所有这些,而不仅仅是查询字符串的文本。

所以我尝试了 parse_str() 函数,它应该能够解析查询字符串本身:

parse_str($query, $myArray);
return $myArray[0];

但这根本不起作用,它在结果页面(网格)中没有给我任何结果。

我做错了什么?首先使用第一种方法获取上面的查询,其次使用将查询字符串解析为其组件? (0索引只是一个例子,我想如果我设法只取出文本部分,我会稍后将其连接起来)?

I am a php newbie, and I'm trying to follow suggestions for how to get the query string. I want the words searched on in a readable format.

Now, the strings come from a database, where they have been previously collected using the request object's referer property in Asp.Net. But as far as I know that shouldn't make any difference.

So I tried this:

function getQuery($url)
{
        $processed_url = parse_url( $url );

        $query_string = $processed_url['query'];
        if($url != '')
        {
            return $query_string;
        }
        else
        {
            return $url;
        }
}

So I tried a variation, which should also extract the query string:

    $query = parse_url($url, 6);

    return $query;

Well, that sort of works. It gives me a query string part, but including the "q=" and all that, not just the text of the query string.

So I tried the parse_str() function on this, which is supposed to be able to parse the query string itself:

parse_str($query, $myArray);
return $myArray[0];

But that didn't work at all, it gives me no results in the result page (a grid).

What am I doing wrong? First of all with the first method for getting the query above, and secondly for parsing the query string into its components? (the 0 index was just an example, I thought I'd concatenate it later if I managed to get out only the text parts)?

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

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

发布评论

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

评论(1

与酒说心事 2024-12-14 01:28:16

看来您正在尝试从字符串解析 url,而不是从当前请求解析 url(如评论中所述)

在评论中的 PHP 文档上,有一个很棒的函数,它完全可以满足您的要求,我已复制它在这里,所以它可以在将来的参考中使用 - 所有功劳都归于“Simon D” - http://dk.php.net/manual/en/function.parse-url.php#104527

<?php 
/** 
 * Returns the url query as associative array 
 * 
 * @param    string    query 
 * @return    array    params 
 */ 
function convertUrlQuery($query) { 
    $queryParts = explode('&', $query); 

    $params = array(); 
    foreach ($queryParts as $param) { 
        $item = explode('=', $param); 
        $params[$item[0]] = $item[1]; 
    } 

    return $params; 
} 
?>

这意味着它应该像这样使用:

$array = convertUrlQuery(parse_url($url, 6));

It would seem that you are trying to parse a url from a string, not from the current request (as mentioned in comment)

On the PHP docs in the comments, there is a great function, that does exactly what you want, i have copied it here, so it can be used in future references - all credit goes to "Simon D" - http://dk.php.net/manual/en/function.parse-url.php#104527

<?php 
/** 
 * Returns the url query as associative array 
 * 
 * @param    string    query 
 * @return    array    params 
 */ 
function convertUrlQuery($query) { 
    $queryParts = explode('&', $query); 

    $params = array(); 
    foreach ($queryParts as $param) { 
        $item = explode('=', $param); 
        $params[$item[0]] = $item[1]; 
    } 

    return $params; 
} 
?>

which means it should be used like this:

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