ninja_forms_csv_explode

Covert a multi-line CSV string into a 2d array.

Description

(array) ninja_forms_csv_explode( (string) $str, (string) $d = ',', (string) $e = '"', (bool) $crlf = true ); 

Follows RFC 4180, allows "cells with ""escaped delimiters""" && multi-line enclosed cells It assumes the CSV file is properly formatted, and doesn't check for errors in CSV format.

Returns (array)

Parameters (4)

0. $str (string)
The CSV string
1. $d — Optional. (string) => ','
The delimiter between values
2. $e — Optional. (string) => '"'
The enclosing character
3. $crlf — Optional. (bool) => true
Set to true if your CSV file should return carriage return

Usage

  1. if ( !function_exists( 'ninja_forms_csv_explode' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'ninja-forms/deprecated/includes/admin/ajax.php'; 
  3.  
  4. // The CSV string 
  5. $str = ''; 
  6.  
  7. // The delimiter between values 
  8. $d = ','; 
  9.  
  10. // The enclosing character 
  11. $e = '"'; 
  12.  
  13. // Set to true if your CSV file should return carriage return 
  14. // and line feed (CRLF should be returned according to RFC 4180 
  15. $crlf = true; 
  16.  
  17. // NOTICE! Understand what this does before running. 
  18. $result = ninja_forms_csv_explode($str, $d, $e, $crlf); 
  19.  

Defined (1)

The function is defined in the following location(s).

/deprecated/includes/admin/ajax.php  
  1. function ninja_forms_csv_explode( $str, $d=', ', $e='"', $crlf=TRUE ) { 
  2. // Convert CRLF to LF, easier to work with in regex 
  3. if( $crlf ) $str = str_replace("\r\n", "\n", $str); 
  4. // Get rid of trailing linebreaks that RFC4180 allows 
  5. $str = trim($str); 
  6. // Do the dirty work 
  7. if ( preg_match_all( 
  8. '/(?: 
  9. '.$e.'((?:[^'.$e.']|'.$e.$e.')*+)'.$e.'(?:'.$d.'|\n|$) 
  10. # match enclose, then match either non-enclose or double-enclose 
  11. # zero to infinity times (possesive), then match another enclose,  
  12. # followed by a comma, linebreak, or string end 
  13. | ####### OR ####### 
  14. ([^'.$d.'\n]*+)(?:['.$d.'\n]|$) 
  15. # match anything thats not a comma or linebreak zero to infinity 
  16. # times (possesive), then match either a comma or a linebreak or 
  17. # string end 
  18. )/x',  
  19. $str, $ms, PREG_SET_ORDER 
  20. ) === FALSE ) return FALSE; 
  21. // Initialize vars, $r will hold our return data, $i will track which line we're on 
  22. $r = array(); $i = 0; 
  23. // Loop through results 
  24. foreach( $ms as $m ) { 
  25. // If the first group of matches is empty, the cell has no quotes 
  26. if( empty($m[1]) ) 
  27. // Put the CRLF back in if needed 
  28. $r[$i][] = ($crlf == TRUE) ? str_replace("\n", "\r\n", $m[2]) : $m[2]; 
  29. else { 
  30. // The cell was quoted, so we want to convert any "" back to " and 
  31. // any LF back to CRLF, if needed 
  32. $r[$i][] = ($crlf == TRUE) ? 
  33. str_replace( 
  34. array("\n", $e.$e),  
  35. array("\r\n", $e),  
  36. $m[1]) : 
  37. str_replace($e.$e, $e, $m[1]); 
  38. // If the raw match doesn't have a delimiter, it must be the last in the 
  39. // row, so we increment our line count. 
  40. if( substr($m[0], -1) != $d ) 
  41. $i++; 
  42. // An empty array will exist due to $ being a zero-length match, so remove it 
  43. array_pop( $r ); 
  44. return $r; 
  45.