我制作了一个脚本,该脚本创建了一个从页面中抓取的 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;
});
如果您同时需要 http
和 https
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/