SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    752
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question Reading CSV and getting email

    Hi

    I have written the following code to get the position of the email address from a csv

    PHP Code:
        function getEmailPosition($delimiter=','$line$debug=false)
        {

            if(
    $delimiter){
                
    // we explode the line based on the delimiter passed.
                
    $explode explode($delimiter$line);
                
    $emailPosition null;
                
    $columnCounter 1;
                
                
                foreach(
    $explode as $column){

                    
    $column trim($column);
                    
    // replace all single and double quotes from the word so that we can check for email validation
                    
    $column str_replace( array( '<''>') , ''$column);

                    if(
    $debuge"<B>Column *{$columnCounter}*:</B> $column<BR>");

                    
    // check if the string is a valid email, if valid, we will know the position of the email column
                    
    if(filter_var($columnFILTER_VALIDATE_EMAIL)){
                        
                        
    $emailPosition $columnCounter;
                        break;
                    }
                    
                    
    $columnCounter++;
                }

                return 
    $emailPosition;
                
            }
            
            return 
    false;
            
        } 
    // end getEmailPosition



    //usage
    $fileContents file('test.csv');
                    foreach(
    $fileContents as $line){
                        
    $emailPosition getEmailPosition(','$linefalse);
                        if( 
    $emailPosition) break;
                    } 
    Contents of the test.csv file:
    Code:
    name, address, email, tel
    john, "55, abc street" , john@gmail.com, 123456
    peter, '118, new street', pet@hotmail.com, 968574

    The above function works fine for cases where there are no single / double quotes, but like in the above case, it splits up the comma found inside the quotes which give wrong column position.

    Is there a better way to get the email position?

    Thanks

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,220
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Try replacing
    PHP Code:
    $explode explode($delimiter$line); 
    With
    PHP Code:
    $explode str_getcsv($line$delimiter); 
    If my understanding of that function is correct, it will handle the quotes for you automatically.

  3. #3
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    752
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Seems like your trick solved my problem.

    Many thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •