How to exclude specific Urls from output?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
soul_fly
Forum Newbie
Posts: 22
Joined: Mon Aug 24, 2009 2:24 pm

How to exclude specific Urls from output?

Post by soul_fly »

Many thanks to McInfo and Chris Corbyn's Regex Crash Course. At last I learnt how to grab urls from a webpage using preg_match.

The problem Now: I want to prune my output a bit. I got 60+ urls from a webpage like as follows:

http://devnetwork.com/hello.html
http://devnetwork.com/php.html
http://devnetwork.com/images/hello.jpg
so on....

During outputting results by print_r(), I just want to exclude all links that contains folder called images. (http://devnetwork.com/images/*)

Hope that i'm clear :)
thx in advance for your co-operation.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to exclude specific Urls from output?

Post by requinix »

If you create a function that can "reject" URLs then you can use array_filter to remove bad URLs from your array.

(Could modify the regex but my opinion would be not to.)
soul_fly
Forum Newbie
Posts: 22
Joined: Mon Aug 24, 2009 2:24 pm

Re: How to exclude specific Urls from output?

Post by soul_fly »

tasairis wrote:If you create a function that can "reject" URLs then you can use array_filter to remove bad URLs from your array.

(Could modify the regex but my opinion would be not to.)
I don't want to modify my regex either. Array filter is good idea. Thanks for your great hint.

My other folks, any other better idea, plz let me know also :)
soul_fly
Forum Newbie
Posts: 22
Joined: Mon Aug 24, 2009 2:24 pm

Re: How to exclude specific Urls from output?

Post by soul_fly »

ok. I have read http://php.net/manual/en/function.array-filter.php but couldn't figure out how to use array_filter() to exclude url from output. :?

The problem background:
$matches is the variable contains the results.
print_r($matches) <--- contains those 60+ Urls

I want to exclude all links contain http://devnetwork.net/images/* from output.

:roll:
soul_fly
Forum Newbie
Posts: 22
Joined: Mon Aug 24, 2009 2:24 pm

Re: How to exclude specific Urls from output?

Post by soul_fly »

knock knock. any volunteer to solve this prob?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to exclude specific Urls from output?

Post by requinix »

tasairis wrote:If you create a function that can "reject" URLs
That was a hint. Have you created that function yet? It takes a URL as a string and returns true or false if the URL should be kept.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How to exclude specific Urls from output?

Post by John Cartwright »

To add to tasairis, you can specify a callback function to array_filter() to handle your "logic" and remove the appropriate array elements.
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: How to exclude specific Urls from output?

Post by mayanktalwar1988 »

hey u can do it like this

Code: Select all

<?
function url($var) {
 if(strstr($var, "image"))
      return false;
else
      return true;
      }
$array1 = array("www.dev.com/hello.php", "www.dev.com/image/hello.jpg","www.dev.com/image/mayank.jpg");
 
print_r(array_filter($array1, "url"));
?>
Post Reply