sohotcall

Siesvi -- PHP CSV Parser (Generator)

Mar 2nd, 2021
673
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. function siesvi( $fname, $mode = 0 ) {
  3.     $fh = fopen( $fname, 'r' );
  4.     $headers = array();
  5.     try {
  6.         while ( ( $str = fgets( $fh ) ) !== false ){
  7.             $quote = 0;
  8.             $cell = "";
  9.             $row = array();
  10.             for ( $i = 0, $strlen = strlen( $str ); $i < $strlen; $i++ ){
  11.                 $chr = $str[$i];
  12.                 if ( $quote == 0 && $chr == "," ){
  13.                     $row[] = $cell;
  14.                     $cell = "";
  15.                 } elseif ( $quote == 0 && $chr == '"' ){
  16.                     $quote = 1;
  17.                 } elseif ( $quote == 1 && $chr == '"' ){
  18.                     if ( $str[ $i + 1 ] == '"' )
  19.                         $cell .= $chr;
  20.                     $quote = 0;
  21.                 } elseif ( $quote == 1 && $chr == "\n" ){
  22.                     $cell .= $chr;
  23.                     $str .= fgets( $fh );
  24.                     $strlen = strlen( $str );
  25.                 } elseif ( $quote == 1 || ( $chr != "\r" && $chr != "\n" )) {
  26.                     $cell .= $chr;
  27.                 }
  28.             }
  29.             $row[] = $cell;
  30.             if ( !( $mode & 4 ) && ! $headers ){
  31.                 $headers = $row;
  32.                 continue;
  33.             }
  34.             if ( $mode & 1 || ! $headers ){
  35.                 if ( $mode & 2 )
  36.                     yield (object) $row;
  37.                 else
  38.                     yield $row;
  39.             } else {
  40.                 if ( $mode & 2 )
  41.                     yield (object) array_combine( $headers, $row );
  42.                 else
  43.                     yield array_combine( $headers, $row );
  44.             }
  45.         }
  46.     } catch( Exception $e ){
  47.         echo "EXCEPTION: " . $e->getMessage();
  48.     } finally {
  49.         fclose( $fh );
  50.     }
  51. }
  52. /*
  53. file_put_contents( __DIR__ . "/test.csv", <<<'EOD'
  54. id,"name",price,"notes"
  55. 1,"kucing ""kampung""",1000,"""Suaranya"" ""meong""
  56. ""meong"""
  57. 2,ayam", "dilepeh"""",2000,"Suaranya sreng-sreng, tadinya, pas digoreng.."
  58. EOD
  59. );
  60.  
  61. foreach ( range(0,7) as $mode ){
  62.     echo sprintf( "MODE = %s\r\n", $mode );
  63.     foreach( siesvi( __DIR__ . "/test.csv", $mode ) as $k => $v) {
  64.         echo sprintf( "Data ke-%s\r\n", $k );
  65.         print_r( $v );
  66.         echo "\r\n";
  67.         //if ($k==0) break;
  68.     }
  69.     echo "----\r\n";
  70. }
  71. */
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×