理解“parse_str”在 PHP 中

发布于 2024-12-07 12:26:37 字数 1757 浏览 0 评论 0原文

我是一名 PHP 新手,试图找到一种使用方法 parse_str 从数据库中解析多个 URL(注意:不是来自请求,它们已经存储在数据库中,不要问...所以 _GET 不起作用)

所以我正在尝试这个:

    $parts = parse_url('http://www.jobrapido.se/?w=teknikinformat%C3%B6r&l=malm%C3%B6&r=auto');
    parse_str($parts['query'], $query);
    return $query['w'];

请注意,我在这里我只是提供一个示例 URL,在实际应用程序中,该 URL 将作为参数从数据库传入。如果我这样做,效果很好。但是,我不明白如何正确使用这个功能,以及如何避免错误。

首先,这里我使用“w”作为返回的索引,因为我可以清楚地看到它在查询中。但这些东西是如何运作的呢?是否有一组特定值可以用来获取整个查询字符串?我的意思是,如果我进一步观察,我也可以在这里看到“l”和“r”...

当然我也可以提取它们并连接结果,但是这些值名称是任意的,还是有办法知道到底要提取哪些?当然,还有“q”值,我最初认为这是我唯一需要的值,但显然不是。它甚至不在示例 URL 中,尽管我知道它在许多其他 URL 中。

那么我该怎么做呢?这就是我想要的:

  1. 提取查询字符串的所有部分,为我提供 URL 搜索字符串部分的可读输出(因此在上面它将是“teknikinformatör Malmö auto”。请注意,我需要翻译 URL 编码瑞典语字符,在 PHP 中有什么简单的方法可以做到这一点吗?)
  2. 处理错误,以便如果上述方法由于某种原因不起作用,该方法应该只返回一个空字符串,从而不会破坏代码。因为此时,如果我将上面的内容与实际参数 $url 一起使用,而不是示例 URL,我会收到错误,因为许多 URL 没有“ w”参数,有的可能是数据库中的空字段,有的可能是格式错误等。那么如何稳定地处理此类错误,如果解析成功就返回一个值,否则返回空字符串?

似乎出现了一个非常奇怪的问题,我在调试过程中看不到。我将这个测试代码放入只是为了看看发生了什么:

function getQuery($url)
{
    try
    {
        $parts = parse_url($url);
        parse_str($parts['query'], $query);
        if (isset($query['q'])) {
            /* return $query['q']; */
            return '';
        }
    } catch (Exception $e) {
        return '';
    }
}

现在,显然在实际代码中我希望返回类似注释掉的部分的内容。然而,令人费解的是:

使用这段代码,据我所知,每条路径都应该返回一个空字符串。但这不起作用 - 它在结果页面中给了我一个完全空的网格。调试期间没有错误或任何内容,并且当我在调试期间单步执行对象时,对象看起来很好。

但是,如果我从该方法中删除除 return '' 之外的所有内容;然后它工作正常 - 当然,查询应该所在的网格中的字段是空的,但所有其他字段都具有应有的所有信息。所以这只是一个测试。但是,为什么应该只能返回空字符串的代码不起作用,而只返回空字符串而不执行其他任何操作的代码却起作用呢?我彻底糊涂了...

I'm a PHP newbie trying to find a way to use parse_str to parse a number of URLs from a database (note: not from the request, they are already stored in a database, don't ask... so _GET won't work)

So I'm trying this:

    $parts = parse_url('http://www.jobrapido.se/?w=teknikinformat%C3%B6r&l=malm%C3%B6&r=auto');
    parse_str($parts['query'], $query);
    return $query['w'];

Please note that here I am just supplying an example URL, in the real application the URL will be passed in as a parameter from the database. And if I do this it works fine. However, I don't understand how to use this function properly, and how to avoid errors.

First of all, here I used "w" as the index to return, because I could clearly see it was in the query. But how do these things work? Is there a set of specific values I can use to get the entire query string? I mean, if I look further, I can see "l" and "r" here as well...

Sure I could extract those too and concatenate the result, but will these value names be arbitrary, or is there a way to know exactly which ones to extract? Of course there's the "q" value, which I originally thought would be the only one I would need, but apparently not. It's not even in the example URL, although I know it's in lots of others.

So how do I do this? Here's what I want:

  1. Extract all parts of the query string that gives me a readable output of the search string part of the URL (so in the above it would be "teknikinformatör Malmö auto". Note that I would need to translate the URL encoding to Swedish characters, any easy way to do that in PHP?)
  2. Handle errors so that if the above doesn't work for some reason, the method should only return an empty string, thus not breaking the code. Because at this point, if I were to use the above with an actual parameter, $url, passed in instead of the example URL, I would get errors, because many of the URLs do not have the "w" parameter, some may be empty fields in the database, some may be malformed, etc. So how can I handle such errors stably, and just return a value if the parsing works, and return empty string otherwise?

There seems to be a very strange problem that occurs that I cannot see during debugging. I put this test code in just to see what is going on:

function getQuery($url)
{
    try
    {
        $parts = parse_url($url);
        parse_str($parts['query'], $query);
        if (isset($query['q'])) {
            /* return $query['q']; */
            return '';
        }
    } catch (Exception $e) {
        return '';
    }
}

Now, obviously in the real code I would want something like the commented out part to be returned. However, the puzzling thing is this:

With this code, as far as I see, every path should lead to returning an empty string. But this does not work - it gives me a completely empty grid in the result page. No errors or anything during debugging, and objects look fine when I step through them during debugging.

However, if I remove everything from this method except return ''; then it works fine - of course the field in the grid where the query is supposed to be is empty, but all the other fields have all the information as they should. So this was just a test. But how is it possible that code that should only be able to return an empty string does not work, while the one that only returns an empty string and does nothing else does work? I'm thoroughly confused...

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

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

发布评论

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

评论(2

满栀 2024-12-14 12:26:37

查询参数的含义完全取决于处理 URL 的应用程序,因此没有“正确”的参数 - 它可能是 wq搜索查询。您可以启发式搜索最常见的变量(=猜测),或返回所有参数的数组。这取决于您想要实现的目标。

parse_str 已经解码了 urlencoding。请注意,urlencoding 是一种编码字节的方法,而不是字符。这取决于应用程序期望的编码。通常(在此示例查询中),到处都应该是 UTF-8,因此您应该覆盖 1。

测试该值是否存在,如果不存在,则返回空字符串,如下所示:

$heuristicFields = array('q', 'w', 'searchquery');
foreach ($heuristicFields as $hf) {
  if (isset($query[$hf])) return $query[$hf];
}
return '';

该函数返回 null 如果输入有效,则当 URL 明显无效时会出现错误(即显示警告消息)。 try...catch 块无效。

The meaning of the query parameters is entirely up to the application that handles the URL, so there is no "right" parameter - it might be w, q, or searchquery. You can heuristically search for the most common variables (=guess), or return an array of all arguments. It depends on what you're trying to achieve.

parse_str already decodes urlencoding. Note that urlencoding is a way to encode bytes, not characters. It depends on what encoding the application expects. Usually (and in this example query), that should be UTF-8 everywhere, so you should be covered on 1.

Test whether the value exists, and if not, return the empty string, like this:

$heuristicFields = array('q', 'w', 'searchquery');
foreach ($heuristicFields as $hf) {
  if (isset($query[$hf])) return $query[$hf];
}
return '';

The function returns null if the input is valid, and runs into errors (i.e., displays warning messages) when the URL is obviously invalid. The try...catch block has no effect.

冰雪梦之恋 2024-12-14 12:26:37

事实证明问题出在瑞典语字符上 - 如果我使用 在返回值之前对值进行 utf8_encode() ,效果很好。

It turned out the problem was with Swedish characters - if I used utf8_encode() on the value before returning it, it worked fine.

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