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


How to find out if the CSV file field tabs are delimited or are delimited with a comma, I need PHP validation. Can anyone help plz thanks in advance?

It is too late to answer this question, but hopefully it will help someone

This is a simple task that will return the delimiter of a file.

  function getFileDelimiter ($ file, $ checkLines = 2) {$ file = new SplFileObject ($ file); $ Delimiter = array (',', '\ t', ';', '|', ':'); $ Result = array (); $ I = 0; While ($ file-> Valid () & amp; $ i & lt; = $ checkLines) {$ line = $ file-> Fgets (); Forex currency ($ delimiter $ as delimiter) {$ regExp = '/['.$delimiter.']/'; $ Fields = preg_split ($ regExp, $ line); If (count ($ field) & gt; 1) {if (empty! ($ Result [$ delimiter])) {$ result [$ delimiter] ++; } And {$ result [$ delimiter] = 1; }}} $ I ++; } $ Result = array_keys ($ result, maximum ($ result)); Returns $ result [0]; }  

Use this function as shown below:

  $ delimiter = getFileDelimiter ('abc.csv'); // Check the 2 lines to set the delimiter $ delimiter = getFileDelimiter ('abc.csv', 5); // Check the 5 lines to set the delimiter  

PS I used preg_split () instead of explosion () because the explosion ('\ t', $ value ) Will not give proper results.

Update: Thanks for @RichardEB, pointing to a bug in code. I have updated it now.


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -