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