0

I have a array of links and i have another array which contains certain values i would like to filter in the list eg:

http://www.liquidshredder.co.uk/shop%3Fkw%3Dsurfboards%26fl%3D330343%26ci%3D3889610385%26network%3Ds
http://www.bournemouth-surfing.co.uk/index.php%3FcPath%3D32
http://www.stcstores.co.uk/wetsuit-range-sizing--pricing-info-1-w.asp
http://www.kingofwatersports.com/wetsuit-sale-c227.html
http://www.uk.best-price.com/search/landing/query/bodyboards/s/google/altk/Surf%2Band/koid/1944273223/
http://www.surfinghardware.co.uk/Results.cfm%3Fcategory%3D20%26kw%3Dbodyboards%26fl%3D11407%26ci%3D3326979552%26network%3Ds
http://www.teste.co.uk/adtrack/baod.html
http://www.teste.co.uk/bodyboards/
www.sandskater.co.uk/
www.sandskater.co.uk/bodyboards/+Bodyboards&sa=X&ei=GwSWS-KaGM24rAeF-vCKDA&ved=0CBMQHzAKOAo
http://www.extremesportstrader.co.uk/buy/water/bodyboarding/
www.extremesportstrader.co.uk/buy/water/bodyboarding/+Bodyboards&sa=X&ei=GwSWS-KaGM24rAeF-vCKDA&ved=0CBYQHzALOAo
www.circle-one.co.uk/+Bodyboards&sa=X&ei=GwSWS-KaGM24rAeF-vCKDA&ved=0CBkQHzAMOAo
http://www.teste.co.uk/bodyboards/p1
http://www.teste.co.uk/bodyboards/p2
http://www.amazon.co.uk/s/%3Fie%3DUTF8%26keywords%3Dbodyboards%26tag%3Dgooghydr-21%26index%3Daps%26hvadid%3D4764625891%26ref%3Dpd_sl_2lyzsfw1ar_e
http://www.teste.co.uk/bodyboards/p3
www.extremesportstrader.co.uk/buy/water/

and i would like to remove all the instances of "http://www.teste.co.uk"?

i tried the below code but it doesn't work :(

$remove=array("teste.co.uk","127.0.0.1","localhost","wikipedia.org","gmail.com","answers.yahoo.com");

foreach ($list[0] as $key=>$clean)
{
    if (in_array($clean,$remove)) 
    {               
        unset($list[0][$key]);
    }

    echo $clean;
    echo '<br>';   
}
4
  • What does a print_r($list[0]) look like? Commented Mar 9, 2010 at 8:37
  • where do you define $list[0] and $urls? Commented Mar 9, 2010 at 8:38
  • @Thomas Ahle : It shows all the urls placed in the array. @erenon : Small mistake $urls should be @clean. the $list is taken from a separate file. Commented Mar 9, 2010 at 8:51
  • I guess I just wander what the rest of $list contains. Commented Mar 9, 2010 at 20:57

4 Answers 4

3

According to Sjoerd:

$url = parse_url($clean);
$host = $url['host'];

if (in_array($host,$remove)) 
{               
    unset($list[0][$key]);
}

Use the parse_url() function to analyse the urls.

Sign up to request clarification or add additional context in comments.

Comments

0

in_array($clean,$remove) only works when the strings match exactly. So if $remove contains "teste.co.uk" and $clean contains "http://www.teste.co.uk/bodyboards/p3", it returns false.

Comments

0
for($i=0; $i < count($array); $i++)
{
  //checks for full match
  if($array[$i] == $key)
  //use if(strpos($array[$i], $key) !== FALSE) to check for substrings.
  {
    array_splice($array, $i, 1)
    $i--;
  }
}

Comments

0

If you array $list[0] is the list you printed, your problem will be that http://www.teste.co.uk/bodyboards/p1 is not in array("teste.co.uk","127.0.... Only the exact string "teste.co.uk" is.

Thus you need to do this:

$remove=array("teste.co.uk","127.0.0.1","localhost","wikipedia.org","gmail.com","answers.yahoo.com");

foreach ($list[0] as $key=>$url) {
    // We have to test for each badword individually
    foreach ($remove as $bad) {
        // Using this strpos trick, we can test if $bad is a substring of $url
        if (strpos($url, $bad) !== false) {
            unset($list[0][$key]);
            break;
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.