开发者

PHP - Search array in array

开发者 https://www.devze.com 2023-03-09 02:58 出处:网络
I have tried googling for the past one hour straight now and tried many ways to search for an array, in an array.

I have tried googling for the past one hour straight now and tried many ways to search for an array, in an array.

My objective is, to find a keyword in the URL, and the keywords are in a txt file.

This is what i have so far - but doesn't work.

$file = "keywords.txt";
$open = fopen($file,'r');
$data = frea开发者_如何学运维d($open,filesize($file));
$data = explode(" ",$data);
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$url = parse_url($url); //parse the URL into an array

foreach($data as $d)
{
    if(strstr($d,$url))
    {
    echo "yes";
    }
}

This works WITHOUT the text file, or array - but that's not what i want.

I'd appreciate it if anyone can assist me.


This is the way I'd do it:

$file = "keywords.txt";
$open = fopen($file,'r');
$data = fread($open,filesize($file));
$data = explode(" ",$data);
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$url = parse_url($url); //parse the URL into an array
foreach($data as $d){
  if(in_array($d,$url)){
    echo "yes";
  }
}
0

精彩评论

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