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. 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. How do I do this?
If I understand correctly, you want to get only fully-qualified (absolute) URLs:
$filtered = array_filter($urls, function($url) {
if (strpos($url, 'http://') === 0) return true;
return false;
});
If you want both http
and https
urls:
$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?
IMHO, ideally, use a regular expression or, if possible, find the specific string with strpos, which is more efficient.
If I understand you correctly, you either want to get the url -- if it exists in the array -- or else NULL
. This PHP code would do that:
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
.
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;
?>