0

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 = 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(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.

3
  • Were you able to get it working Anonymous2011? Commented May 30, 2011 at 22:30
  • what do you mean when you say its not working.what are u expecting and what is it giving? Commented May 30, 2011 at 23:03
  • I tried using in_array() like vap0r's method below, but that didn't work for some reason, although i'm sure that's what i did before... What i meant by it not working, was the expected outcome was php to echo "yes" Commented May 30, 2011 at 23:06

1 Answer 1

0

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";
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Vap0r, it worked perfectly - I'm more than sure i tried that method before (in_array()) lol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.