查询字符串的关键字提取器

发布于 2024-10-05 20:51:53 字数 779 浏览 0 评论 0原文

我找到了一些用于查询字符串的关键字提取器,但它们似乎都已过时(使用已弃用的代码或根本不起作用)。

有谁知道 php 查询字符串提取器。或者我将如何构建一个函数,该函数接受“amazon.com”等“主机”和多个查询参数的名称并返回这些参数的值?

例如,http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=a+tale+of+two+cities&x=0&y= 0

如果我提供了“amazon.com”和“field-keywords”,如何获取数组:['a','tale','of','two','cities'] ?

提前致谢!!

澄清

Jose 的回答涵盖了我针对 Amazon 的示例,但似乎不适用于任何其他 URL:

http://www.bing.com/search?q=Christmas+Around+The+ World&form=QBLH&qs=PN&sk=HS1PN4&pq=&sp=6&sc=8-0

http://search.yahoo.com/search;_ylt=AnQN0c997QR5SIYcYt。 h2YCbvZx4?p=golf&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-701

I've found a few keyword extractors for query strings, but they all seem to be outdated (use deprecated code or simply don't work).

Does anyone know of a php query string extractor. Or how would I build a function that takes a "host" like "amazon.com" and the names of multiple query parameters and returns the values of those parameters?

For instance, http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=a+tale+of+two+cities&x=0&y=0

If I provided "amazon.com" and "field-keywords" how can I get the array: ['a','tale','of','two','cities'] ?

Thanks in advance!!

Clarification

Jose's answer covers my example for Amazon but doesn't seem to work for any other URL:

http://www.bing.com/search?q=Christmas+Around+The+World&form=QBLH&qs=PN&sk=HS1PN4&pq=&sp=6&sc=8-0

http://search.yahoo.com/search;_ylt=AnQN0c997QR5SIYcYt.h2YCbvZx4?p=golf&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-701

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

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

发布评论

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

评论(3

断肠人 2024-10-12 20:51:53

你(我)可以写一个。

function getValueFromDomain($urls, $domain, $key) {
    foreach ($urls as $url) {
        if (preg_match('/https?:\/\/[^\/]*?' . preg_quote($domain) . '\//', $url)) {
            parse_str(substr($url, strpos($url, '?') + 1), $output);
            if (isset($output[$key])) {
                $array = explode(' ', $output[$key]);
                return $array;
            }
        }
    }
    return array();
}

只是从 @Jose 的答案中窃取了一点代码:)

更新:经过测试,不起作用,修复,起作用。

更新2:毕竟没用,经过测试、修复、有效。

更新 3:添加了对 HTTPS 的支持

You (I) could write one.

function getValueFromDomain($urls, $domain, $key) {
    foreach ($urls as $url) {
        if (preg_match('/https?:\/\/[^\/]*?' . preg_quote($domain) . '\//', $url)) {
            parse_str(substr($url, strpos($url, '?') + 1), $output);
            if (isset($output[$key])) {
                $array = explode(' ', $output[$key]);
                return $array;
            }
        }
    }
    return array();
}

Just a tad of code stolen from @Jose's answer :)

Update: tested, didn't work, fixed, works.

Update 2: didn't work after all, tested, fixed, works.

Update 3: added support for HTTPS

酒解孤独 2024-10-12 20:51:53

这个应该可以..

<?php
    $str = "http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=a+tale+of+two+cities&x=0&y=0";
    parse_str($str, $output);
    $array =  explode(" ",$output['field-keywords']);
    print_r($array);

?>

This should do it..

<?php
    $str = "http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=a+tale+of+two+cities&x=0&y=0";
    parse_str($str, $output);
    $array =  explode(" ",$output['field-keywords']);
    print_r($array);

?>
只是一片海 2024-10-12 20:51:53

按照我阅读问题的方式,您只需要将查询字符串索引名称传递给函数,例如......

function get_keywords($parameter){
        $words = explode(" ", $_GET[$parameter]);
         return $words;
}

The way I read the question, you would only need the query string index name to be passed to the function like...

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