fork download
  1. <?php
  2.  
  3. function str_to_csv( $row )
  4. {
  5. if( $row=='' )
  6. {
  7. return array();
  8. }
  9. $a = array();
  10. $src = explode(',', $row );
  11. do{
  12. $p = array_shift($src);
  13. while( mb_substr_count($p,'"') % 2 != 0 )
  14. {
  15. if( count($src)==0 ){ return false; }
  16. $p .= ','.array_shift($src);
  17. }
  18. $match = null;
  19. if( preg_match('/^"(.+)"[
  20. ]*$/', $p, $match ))
  21. {
  22. $p = $match[1];
  23. }
  24. $a[] = str_replace('""','"',$p);
  25. }while( count($src) > 0 );
  26. return $a;
  27. }
  28.  
  29.  
  30. function file_getcsv( $f )
  31. {
  32. $line = fgets( $f );
  33. while( ($a = str_to_csv($line))===false )
  34. {
  35. if( feof($f) ){ return false; }
  36. $line .= "\n".fgets( $f );
  37. }
  38. return $a;
  39. }
  40.  
  41.  
  42. function file_to_csv( $filename )
  43. {
  44. ini_set("auto_detect_line_endings", true);
  45. $a = array();
  46. $f = fopen($filename,'r');
  47. while( !feof($f) )
  48. {
  49. $rec = file_getcsv($f);
  50. if( $rec===false ){ return false; }
  51. if( !empty($rec) )
  52. {
  53. $a[] = $rec;
  54. }
  55. }
  56. fclose($f);
  57. return $a;
  58. }
  59.  
  60. $data = file_to_csv('php://stdin');
  61.  
  62. echo '<pre>';print_r($data);
  63. ?>
Success #stdin #stdout 0.04s 25936KB
stdin
Royal Kings
Mere Cats
Spin Doctors
رأس العين
stdout
<pre>Array
(
    [0] => Array
        (
            [0] => Royal Kings

        )

    [1] => Array
        (
            [0] => Mere Cats

        )

    [2] => Array
        (
            [0] => Spin Doctors

        )

    [3] => Array
        (
            [0] => رأس العين
        )

)