PHP Find string in CSV file

I have a large flat file of usernames and letters in the following format:

"username", "email" "username", "email" "username", "email" 

etc...

I need to take an email and find the username, but for some reason it will not return the result. It works if I search the other way around.

 $string = " user_email@something.com "; $filename = "user_email.txt"; $h = fopen("$filename","r"); $flag=0; while (!feof ($h)) { $buffer = fgets($h); $thisarray = split(",", $buffer); if ($string == str_replace('"','', $thisarray[1])) { $i = 1; $i++; echo '<td bgcolor="#CCFFCC"><b style="color: maroon">' . str_replace('"','',$thisarray[0]). '</b></td>'; } 

Any ideas? Thanks!

+7
source share
6 answers

As per reko_t: suggestion, use fgetcsv to read individual csv lines into arrays until you find one where the second element matches your search query. The first element is the username. Something like:

 <?php function find_user($filename, $email) { $f = fopen($filename, "r"); $result = false; while ($row = fgetcsv($f)) { if ($row[1] == $email) { $result = $row[0]; break; } } fclose($f); return $result; } 
+6
source

You can use fgetcsv() directly

 $string = " user_email@something.com "; $filename = "user_email.txt"; $h = fopen("$filename","r"); $flag=0; while (!feof ($h)) { list($username, $email= fgetcsv($h); if ($string == $email) { /* do something */ } } 

fgetcsv() (as a good side effect) also removes the "field fields" (double quotes " ) for you if they exist.

Your own example probably doesn't work, because if you have such a line

 "username", "email" 

splitting at will result in

 '"username"' ' "email"' 

Pay attention to the spaces before the "email" that you forgot to delete. The additional use of str_replace() to remove surrounding quotes is rather unsafe. Take a look at trim() .

+1
source

First, just use file () to get the contents of the file into an array:

 $file_contents = file( $filename, 'r' ); 

Now swipe the contents of the array by separating the lines and examining the email address:

 foreach ( $file_contents as $line ) { list ( $username, $email ) = str_split( ',' $line ); if ( trim( $email ) == $string ) { // A match was found. Take appropriate action. } } 
0
source

I think the easiest solution is to use file () with str_getcsv ().

The code would be something like this:

 foreach (file($fileName, FILE_SKIP_EMPTY_LINES) as $line) { $columns = str_getcsv($line); // Where $columns[0] and $columns[1] hold the username and email respectively. } 
0
source

While fgetcsv is a potentially more elegant solution, this does not answer your original question: your second element in the array has a new line, and you compare it with a line that does not.

To fix it:

 if ($string == str_replace('"','', chop($thisarray[1]))) { 
0
source

I really believe that all the examples in the other answers work!
But they are all slow because they are all looking at every line in the csv file ...
I have one more example how to find the necessary line:

 $command = sprintf("grep '%s,%s' -Er %s", $userName, $email, $file); $result = `$command`; 

Yes, this is some kind of dark matter, but it really works, and it is really fast !

0
source

All Articles