0

Hey all i am trying to search through a CSV file using the following code:

$fh = fopen($myFile, "r");
$os = array(fgetcsv($fh));

if (in_array("12.56.102.44", $os)) {
    echo "Got it!";
}

The CSV is in this format:

"132.89.14.110","41.98.365.111","12.56.102.44","77.295.10.111",

However, that does not work so i am sure i am using it incorrectly...

Any help would be great!

2
  • 1
    Is the CSV sheet setup so that there are numerous lines, one IP to each line; or one line of numerous columns, one IP in each column? Commented Jul 27, 2012 at 0:32
  • one line of numbers, no breaks no returns and no columns. Commented Jul 27, 2012 at 0:37

1 Answer 1

3

fgetcsv() already returns an array. You should be calling the function like this:

$os = fgetcsv($fh);

Putting the return value inside an array will make the value of $os the following:

array(array("132.89.14.110","41.98.365.111","12.56.102.44","77.295.10.111"))

If there are multiple lines in your file that you need to check, you'll have to make use of a loop, for example:

$fh = fopen($myFile, "r");
while(($os = fgetcsv($fh)) !== false) {
    if (in_array("12.56.102.44", $os)) {
        echo "Got it!";
        break; // stop the loop, we're done
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1. Also should be pointed that fgetcsv parses only 1 line of the csv file
So since fgetcsv returns an array then how do i go about looping that array and finding the ip? And by multiple lines do you mean "ip","ip",etc or a new line for each ip? Because running your code above provides no Got it! results.

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.