【问题标题】:PHP Filtering an array for 1 urlPHP过滤1个url的数组
【发布时间】:2011-07-28 08:21:00
【问题描述】:

我制作了一个脚本,该脚本创建了一个从页面中抓取的 url 数组,我想过滤该数组以获取 1 个特定的 url。 该数组当前如下所示:

Array
(
    [0] => index.jsp
    [1] => feedback.jsp
    [2] => faq.jsp
    [3] => donate.jsp
    [4] => contact.jsp
    [5] => widgetmaker.jsp
    [11] => http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php
    [12] => http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php
    [13] => http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php
    [14] => http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php
    [15] => http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php
)

我想要它做的是获取“http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php”之一链接。我该怎么做?

【问题讨论】:

    标签: php regex arrays web-scraping


    【解决方案1】:

    如果我理解正确,您只想获得完全限定(绝对)的 URL:

    $filtered = array_filter($urls, function($url) {
        if (strpos($url, 'http://') === 0) return true;
        return false;
    });
    

    如果你想要httphttps 两个网址:

    $filtered = array_filter($urls, function($url) {
        if (preg_match('#^https?://#', $url)) return true;
        return false;
    });
    

    如果您只想要完全匹配:

    $filtered = array_filter($urls, function($url) {
        if ($url == 'http://full/url/goes/here') return true;
        return false;
    });
    

    如果你只想得到第一个,那么:

    $url = $filtered[0];
    

    【讨论】:

    【解决方案2】:

    我认为理想的做法是改进脚本以仅捕获一个链接。您知道最终到达网址的标准吗?

    恕我直言,理想情况下,使用regular expression,或者,如果可能,使用strpos 查找特定字符串,这样更有效。

    【讨论】:

    • 我想获取“example.com/myaccount/…”链接,但问题是令牌不同,所以我不确定如何使用正则表达式。
    【解决方案3】:

    如果我对您的理解正确,您要么想要获取 url——如果它存在于数组中——或者NULL。这个 PHP 代码可以做到这一点:

    function get_url_if_present($wanted, $array) {
         return array_keys($array, $wanted) ? $wanted : NULL;
    }
    

    ...其中$wanted 是您在$array 中搜索的url,如果找到的url 存在于数组中,则返回值是一个字符串,否则为NULL

    你可以这样调用这个函数:

    <?php
    
    function get_url_if_present($wanted, $array) {
         return array_keys($array, $wanted) ? $wanted : NULL;
    }
    
    $arr = Array
    (
        0 => "index.jsp",
        1 => "feedback.jsp",
        2 => "faq.jsp",
        3 => "donate.jsp",
        4 => "contact.jsp",
        5 => "widgetmaker.jsp",
        11 => "http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php",
        12 => "http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php",
        13 => "http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php",
        14 => "http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php",
        15 => "http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php"
    );
    
    $url_as_string = get_url_if_present("http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php", $arr);
    print $url_as_string;
    
    ?>
    

    【讨论】:

    • 我想把想要的url变成一个字符串,而且token也不是静态的。
    • 如果 _mska_tok 是静态的,那将非常有效,但我担心它是动态的。你会如何让它抓取包含“example.com/myaccount/accountactivation”的链接?
    猜你喜欢
    • 1970-01-01
    • 2018-05-22
    • 2018-05-04
    • 2018-11-01
    • 1970-01-01
    • 2011-08-28
    • 2016-12-30
    • 2016-07-05
    • 2016-05-24
    相关资源
    最近更新 更多