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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With