2016-08-18 123 views
-2

我有一个数组$链接包含在它里面的一些环节,我想删除所有这些都是从网站出现在$ Blocklinks删除元素

$links=array(
'http://ckfht.ca/sultan/files/2016/', 
'http://dl1.uploadplus.net/dl2/2016/Sultan.2016/', 
'http://www.google.com', 
'http://subindomovie.xyz/film/indexof-sultan-720p' 
'http://subindomovie.xyz/film/sultan-720-p' 
'http://www.liveaccountbook.com/sultan/' 


); 

$Blocklinks=array(
    'subindomovie.xyz', 
    'www.liveaccountbook.com' 
); 
    /* remove urls containing link from $Blocklinks .. What should i do here??*/ 
$links=deletelinks($links,$Blocklinks); 

/* output */ 
print_r($links); 

output I want 
------ 
Array 
(
    [0] => http://ckfht.ca/sultan/files/2016/ 
    [1] => http://dl1.uploadplus.net/dl2/2016/Sultan.2016/ 
    [2] => http://www.google.com 
) 
+1

你有什么试过的?你收到了什么错误信息?如果没有显示错误信息,那么请发布您的PHP错误日志。谢谢 –

+0

您可以使用[strpos函数](http://php.net/manual/en/function.strpos.php)检查字符串是否包含特定子字符串。也使用foreach循环 –

+2

[PHP的:如何从数组中删除特定元素?]可能的副本(http://stackoverflow.com/questions/2448964/php-how-to-remove-specific-element-from-an-数组) – cteski

回答

4

如果链接所有你需要的是通过主机名来过滤的网址,你可以这样来做:

$result = array_filter($links, function ($i) use ($Blocklinks) { 
    return !in_array(parse_url($i, PHP_URL_HOST), $Blocklinks); }); 

如果你想要一个空字符串替换网址:

功能的方法:

$result = array_map(function ($i) use ($Blocklinks) { 
    return in_array(parse_url($i, PHP_URL_HOST), $Blocklinks) ? '' : $i; 
}, $links); 

程序的方式:

$result = []; 
foreach($links as $link) { 
    $result = in_array(parse_url($link, PHP_URL_HOST), $Blocklinks) ? '' : $link; 
} 
+0

以及如果我想让它们变为空白而不是删除它们,我该怎么做? – Kaif

+0

@Kaif:要做到这一点''array_map'更适合。但'array_map'作为'array_filter'只是简单的'foreach'循环的功能版本,您可以在其中存储修改或选择的项目。 –

+0

真棒!!!!!!!!!!!!!!!!!!!!! :D – Kaif

1
$links = array_filter($links, function ($link) use ($Blocklinks) { 
    foreach ($Blocklinks as $block) { 
     if (strstr($link, $block) !== false) { 
      return false; 
     } 
    } 

    return true; 
}); 
0

使用此代码在您的deletelinks()功能...

function deletelinks($links,$Blocklinks) 
{ 
    foreach($links as $key => $link) 
    { 
     foreach($Blocklinks as $bLink) 
     { 
      if (strpos($link,$bLink) !== false) { 
       unset($links[$key]); 
      } 
     } 
    } 
    return $links; 
} 

这将返回:

Array 
(
    [0] => http://ckfht.ca/sultan/files/2016/ 
    [1] => http://dl1.uploadplus.net/dl2/2016/Sultan.2016/ 
    [2] => http://www.google.com 
) 

活生生的例子:CLICK HERE

+0

非常感谢你:) – Kaif

+0

@Kaif欢迎亲爱的,你可以投票回答哪个对你有帮助... –

0

好像你一起,将删除因此deletelinks($links, $Blocklinks)链接功能的思路思考。也许下面的函数派上用场...你也可以try it out here

<?php 

    $links  = array(
       'http://ckfht.ca/sultan/files/2016/', 
       'http://dl1.uploadplus.net/dl2/2016/Sultan.2016/', 
       'http://www.google.com', 
       'http://subindomovie.xyz/film/indexof-sultan-720p', 
       'http://subindomovie.xyz/film/sultan-720-p', 
       'http://www.liveaccountbook.com/sultan/', 
       ); 

    $blockLinks = array(
       'subindomovie.xyz', 
       'www.liveaccountbook.com', 
    ); 

    $newLinks = deleteLinks($links, $blockLinks); 
    var_dump($newLinks); 

    function deleteLinks($links, $blockLinks){ 
     foreach($links as $key=>$link){ 
      foreach($blockLinks as $index=>$blockLink){ 
       if(stristr($link, $blockLink)){ 
        unset($links[$key]); 
       } 
      } 
     } 
     return $links; 
    } 

    // THE var_dump($newLinks) ABOVE WOULD PRODUCE SOMETHING LIKE: 

    array (size=3) 
     0 => string 'http://ckfht.ca/sultan/files/2016/' (length=34) 
     1 => string 'http://dl1.uploadplus.net/dl2/2016/Sultan.2016/' (length=47) 
     2 => string 'http://www.google.com' (length=21) 
+0

非常感谢你:) – Kaif