[英]PHP Filtering an array for 1 url
I made a script that creates an array of urls scraped from a page and I want to filter the array for just 1 certain url. 我创建了一个脚本,它创建了一个从页面中抓取的url数组,我想只过滤一个特定网址的数组。 The array currently looks like this:
该阵列目前看起来像这样:
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
)
And what I want it to do is grab one of the "http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php" links. 我想要它做的是抓住其中一个“http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php”链接。 How do I do this?
我该怎么做呢?
If I understand correctly, you want to get only fully-qualified (absolute) URLs: 如果我理解正确,您只想获得完全限定(绝对)的URL:
$filtered = array_filter($urls, function($url) {
if (strpos($url, 'http://') === 0) return true;
return false;
});
If you want both http
and https
urls: 如果你想要
http
和https
网址:
$filtered = array_filter($urls, function($url) {
if (preg_match('#^https?://#', $url)) return true;
return false;
});
If you only want exact matches: 如果您只想要完全匹配:
$filtered = array_filter($urls, function($url) {
if ($url == 'http://full/url/goes/here') return true;
return false;
});
If you only want to get the first one then: 如果你只想获得第一个:
$url = $filtered[0];
I think the ideal would refine the script to catch just one link. 我认为理想会改进脚本以捕获一个链接。 Do you know the criteria that should be the final URL?
您知道应该是最终URL的标准吗?
IMHO, ideally, use a regular expression or, if possible, find the specific string with strpos , which is more efficient. 理想情况下,恕我直言,使用正则表达式,或者,如果可能,使用strpos查找特定字符串,这样更有效。
If I understand you correctly, you either want to get the url -- if it exists in the array -- or else NULL
. 如果我理解正确,你要么想要获取url - 如果它存在于数组中 - 否则为
NULL
。 This PHP code would do that: 这个PHP代码会这样做:
function get_url_if_present($wanted, $array) {
return array_keys($array, $wanted) ? $wanted : NULL;
}
...where $wanted
is the url you're searching for in $array
, and the return value is a string with the found url if it was present in the array, otherwise NULL
. ...
$wanted
是你在$array
搜索的url,返回值是一个带有找到url的字符串,如果它存在于数组中,否则为NULL
。
You may call this function like this: 你可以这样调用这个函数:
<?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;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.