Check if value exists in external file array php

2246 views php
6

I have this code which is to check if google exist in the external array file, it gives me the blank result but when i transferred the array into the same file or inline the list, it works. I'm using the external array file for global use.

$approveurl = file('../webfilters.php');
if(in_array('http://google.com', $approveurl)){ echo "Success";} 

answered question
Max Muster

what is the external array file ?

Marc Justin Rait

The external array file looks like this vergleich-webhosting.info alltemplate.org executivestudio.net

Max Muster

you want to check the html code of this sites to see if there is the string http://google.com ?

Marc Justin Rait

Yes, the file() code returns the external file into array when i use the var_dump($approveurl) it will show the list into array form.

Barmar

At a minimum you need to use the FILE_IGNORE_NEW_LINES flag, otherwise all the strings in the array will end with newline, so they won't match.

Max Muster

as far as I know in_array checks if you have one complete element in the "haysack" that is equal to the "needle" . for such things I would use file_get_contents and then you use strpos($needle, $haysack)

Marc Justin Rait

Okay, Thank you for this help, i added the code and it works!!

Add a Comment

2 Answers

4
Barmar 0 Comments

There's no need to turn the file into an array. Read it into a string and then use strstr().

$data = file_get_contents("../webfilters.php");
if (strstr($data, "http://google.com")) {
    echo "Success";
}

posted this
8
Hitesh 0 Comments

Use can try like this -

$file   = file('../webfilters.php');

foreach ($file as $key => $value) {

  $temp = explode(',', $value);

  if ($temp[0] == 'http://google.com'){
    echo 'http://google.com is found';
    exit;} }

posted this

Please login first before posting an answer.