Jump to content

array manipulation question


maddogandnoriko

Recommended Posts

Quick and simple:

 

$array = array('www.google.com','www.yahoo.com','www.somesite.com','http://www.google.com','site2.com');
foreach ($array as $link) {
   if (!stristr($link,'google')) {
      $newArray[] = $link;
   }
}

 

The limitation to this is that it will match links that contain 'google' regardless of where in the link it is.  So if you have a link like www.mysite.com/google/somepage.php  or www.mysite.com/somepage.php?site=google or some other link that would have 'google' in it but not actually be a google page, those won't make it to $newArray. 

 

If you are worried about something like that happening, you're going to have to use preg_match instead.  There are a ton of examples in that link and on the net for checking for a valid url.  All you need to do is swap out the wildcard used for the domain name to specifically check for google.

 

Alternatively, you can do $key => $link in the foreach loop and unset($array[$key]) on match, to work with the original array.  Same stristr limitations would apply.

 

Link to comment
Share on other sites

Hi

 

If you have an array that contains all the possible google urls then you could use the array_diff function.

 

Or you could use the array_filter function, and knock up a small function to recognise the urls you want to ignore (recieves the array member and returns true if you want to keep it).

 

All the best

 

Keith

Link to comment
Share on other sites

That's terribly inefficient and downright impossible.

 

Depends on what he means by containing google.com. If he is just interested in excluding their own basic services then it would be fine, but utterly useless as you say for detailed URLs.

 

array_filter would be more appropriate that if he is dealing with lots of seperate google urls, including those with query strings.

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.