PHP Find String in CSV file

后端 未结 6 1153
无人及你
无人及你 2020-12-17 17:36

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

\"username\", \"email\"
\"username\", \"email\"
\"username\", \"email\"
<         


        
6条回答
  •  無奈伤痛
    2020-12-17 17:58

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

提交回复
热议问题