maddogandnoriko Posted March 9, 2009 Share Posted March 9, 2009 I have an array filled with links. Is it possible to get all elements that contain google.com? And if so is it also possible to remove those results from the original array? I am currently doing it with a for each loop and was just wondering if there is a one step solution... todd Quote Link to comment https://forums.phpfreaks.com/topic/148607-array-manipulation-question/ Share on other sites More sharing options...
.josh Posted March 9, 2009 Share Posted March 9, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/148607-array-manipulation-question/#findComment-780394 Share on other sites More sharing options...
kickstart Posted March 9, 2009 Share Posted March 9, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/148607-array-manipulation-question/#findComment-780402 Share on other sites More sharing options...
.josh Posted March 9, 2009 Share Posted March 9, 2009 That's terribly inefficient and downright impossible. www.google.com/ANYFILENAME?ANYVARNAME=ANYVALUE[&ANYVARNAME=ANYVALUE[...]] makes the number of valid google urls infinite. Better to just search for 'google' in the link somewhere or restrict where to look with regex. Quote Link to comment https://forums.phpfreaks.com/topic/148607-array-manipulation-question/#findComment-780413 Share on other sites More sharing options...
kickstart Posted March 9, 2009 Share Posted March 9, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/148607-array-manipulation-question/#findComment-780426 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.