-2

i have an array $link which contains some links inside it and i wanna delete all the links which are from sites present in $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
)
Kaif
  • 21
  • 2
  • 1
    what have your tried? What error messages did you get? If no error messages where displayed then please post your php error log. Thank You – Mark Twigg Aug 18 '16 at 13:31
  • you can check if a string contains a specific substring by using the [strpos function](http://php.net/manual/en/function.strpos.php). Also use foreach loop – Tanuel Mategi Aug 18 '16 at 13:32
  • 2
    Possible duplicate of [PHP: How to remove specific element from an array?](http://stackoverflow.com/questions/2448964/php-how-to-remove-specific-element-from-an-array) – cteski Aug 18 '16 at 13:34
  • "i have'nt tried any function". The order of using this site is 1.) Try something. 2.) Have trouble getting it to work. 3.) Ask a question. We are not here to fulfill your project requirements for you – Patrick Q Aug 18 '16 at 13:34
  • 1
    So do some basic searching. Start with finding out how to iterate through an array. Then find out how to match part of one string with all of another string. – Patrick Q Aug 18 '16 at 13:37

4 Answers4

4

If all you need is to filter urls by hostname, you can do it this way:

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

If you want to replace urls with an empty string:

functional way:

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

procedural way:

$result = [];
foreach($links as $link) {
    $result = in_array(parse_url($link, PHP_URL_HOST), $Blocklinks) ? '' : $link;
}
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
1
$links = array_filter($links, function ($link) use ($Blocklinks) {
    foreach ($Blocklinks as $block) {
        if (strstr($link, $block) !== false) {
            return false;
        }
    }

    return true;
});
Gareth Parker
  • 5,012
  • 2
  • 18
  • 42
0

Use this code in your deletelinks() function...

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

This will return you :

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

LIVE EXAMPLE : CLICK HERE

Manjeet Barnala
  • 2,975
  • 1
  • 10
  • 20
0

Seems like you were thinking along the lines of a function that would delete the links hence the deletelinks($links, $Blocklinks). Perhaps the function below comes in handy... You may as well 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)
Poiz
  • 7,611
  • 2
  • 15
  • 17