Kaif Kaif - 3 months ago 11
PHP Question

deleting elements from array containing specific string keywords

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
)

Answer

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