how to find out if csv file fields are tab delimited or comma delimited

后端 未结 15 1007
[愿得一人]
[愿得一人] 2020-12-01 09:46

how to find out if csv file fields are tab delimited or comma delimited. I need php validation for this. Can anyone plz help. Thanks in advance.

15条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-01 10:36

    Here's what I do.

    1. Parse the first 5 lines of a CSV file
    2. Count the number of delimiters [commas, tabs, semicolons and colons] in each line
    3. Compare the number of delimiters in each line. If you have a properly formatted CSV, then one of the delimiter counts will match in each row.

    This will not work 100% of the time, but it is a decent starting point. At minimum, it will reduce the number of possible delimiters (making it easier for your users to select the correct delimiter).

    /* Rearrange this array to change the search priority of delimiters */
    $delimiters = array('tab'       => "\t",
                    'comma'     => ",",
                    'semicolon' => ";"
                    );
    
    $handle = file( $file );    # Grabs the CSV file, loads into array
    
    $line = array();            # Stores the count of delimiters in each row
    
    $valid_delimiter = array(); # Stores Valid Delimiters
    
    # Count the number of Delimiters in Each Row
    for ( $i = 1; $i < 6; $i++ ){
    foreach ( $delimiters as $key => $value ){
        $line[$key][$i] = count( explode( $value, $handle[$i] ) ) - 1;
    }
    }
    
    
    # Compare the Count of Delimiters in Each line
    foreach ( $line as $delimiter => $count ){
    
    # Check that the first two values are not 0
    if ( $count[1] > 0 and $count[2] > 0 ){
        $match = true;
    
        $prev_value = '';
        foreach ( $count as $value ){
    
            if ( $prev_value != '' )
                $match = ( $prev_value == $value and $match == true ) ? true : false;
    
            $prev_value = $value;
        }
    
    } else { 
        $match = false;
    }
    
    if ( $match == true )    $valid_delimiter[] = $delimiter;
    
    }//foreach
    
    # Set Default delimiter to comma
    $delimiter = ( $valid_delimiter[0] != '' ) ? $valid_delimiter[0] : "comma";
    
    
    /*  !!!! This is good enough for my needs since I have the priority set to "tab"
    !!!! but you will want to have to user select from the delimiters in $valid_delimiter
    !!!! if multiple dilimiter counts match
    */
    
    # The Delimiter for the CSV
    echo $delimiters[$delimiter]; 
    

提交回复
热议问题