开发者

remove values from an array php

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开发者_C百科 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>';   
}


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.


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.


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--;
  }
}


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;
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜