PHP过滤1个url的数组

标签 php regex arrays web-scraping

我制作了一个脚本,该脚本创建了一个从页面中抓取的 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”之一链接。我该怎么做?

最佳答案

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

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

如果您同时需要 httphttps url:

$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];

关于PHP过滤1个url的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5524873/

相关文章:

python - 列出正则表达式匹配的所有模式

Javascript twoSum 算法 : Given an array of integers, 返回两个数字的索引,以便它们加起来达到特定目标

arrays - Mongoose 枚举字符串数组验证?

java.lang.ArrayIndexOutOfBoundsException : 48

php - MySQL php 插入...选择 : use commas to join $var+value

PHP日期和时间问题

regex - Powershell函数在文本文件中替换或添加行

php - 使用 INNER JOIN 从表中检索最新的时间戳行

php - PDO Mysql 没有正确返回结果

Javascript 正则表达式 : remove first and last slash