12

I've got a large flat file of usernames and emails in the following format:

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

etc...

I need to take the email and search for the username, but for some reason it will not return a result. It works if I search opposite.

$string = "[email protected]";
$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!

2

6 Answers 6

12

As per reko_t's suggestion: Use fgetcsv to read individual lines of csv into arrays, until you find one where the second element matches your search term. The first element then 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;
}
Sign up to request clarification or add additional context in comments.

Comments

2

You may use fgetcsv() directly

$string = "[email protected]";
$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 nice side effect) also removes the "field enclosures" (the double quotes ") for you, if they exists.

Your own example probably does not work, because if you have such a line

"username", "email"

splitting at , will result in

'"username"'
' "email"'

Notice the whitespace before "email", that you forgot to remove. Additional using str_replace() to remove the surrounding quotes is quite unsafe. Have a look at trim().

1 Comment

Also, if you only need to go until the email is found, and then stop, add a break; statement to the end of the if statement.
1

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

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

Now loop through the contents of the array, splitting the strings 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.
    }
}

Comments

1

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.
}

Comments

1

I truly believe that all examples in other answers works!
But all they are slow, because all of them travers each line in csv file...
I have another example how to find desired string:

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

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

Comments

0

While fgetcsv is potentially a more elegant solution, that doesn't answer your original question: your second array element has a newline, and you're comparing against a string that doesn't.

To fix:

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

Comments

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.