Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's what I do.</p> <ol> <li>Parse the first 5 lines of a CSV file</li> <li>Count the number of delimiters [commas, tabs, semicolons and colons] in each line</li> <li>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.</li> </ol> <p>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).</p> <pre><code>/* Rearrange this array to change the search priority of delimiters */ $delimiters = array('tab' =&gt; "\t", 'comma' =&gt; ",", 'semicolon' =&gt; ";" ); $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 &lt; 6; $i++ ){ foreach ( $delimiters as $key =&gt; $value ){ $line[$key][$i] = count( explode( $value, $handle[$i] ) ) - 1; } } # Compare the Count of Delimiters in Each line foreach ( $line as $delimiter =&gt; $count ){ # Check that the first two values are not 0 if ( $count[1] &gt; 0 and $count[2] &gt; 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]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload