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
if ( !function_exists( 'ninja_forms_csv_explode' ) ) { require_once ABSPATH . PLUGINDIR . 'ninja-forms/deprecated/includes/admin/ajax.php'; } // The CSV string $str = ''; // The delimiter between values $d = ','; // The enclosing character $e = '"'; // Set to true if your CSV file should return carriage return // and line feed (CRLF should be returned according to RFC 4180 $crlf = true; // NOTICE! Understand what this does before running. $result = ninja_forms_csv_explode($str, $d, $e, $crlf);
Defined (1)
The function is defined in the following location(s).
- /deprecated/includes/admin/ajax.php
- function ninja_forms_csv_explode( $str, $d=', ', $e='"', $crlf=TRUE ) {
- // Convert CRLF to LF, easier to work with in regex
- if( $crlf ) $str = str_replace("\r\n", "\n", $str);
- // Get rid of trailing linebreaks that RFC4180 allows
- $str = trim($str);
- // Do the dirty work
- if ( preg_match_all(
- '/(?:
- '.$e.'((?:[^'.$e.']|'.$e.$e.')*+)'.$e.'(?:'.$d.'|\n|$)
- # match enclose, then match either non-enclose or double-enclose
- # zero to infinity times (possesive), then match another enclose,
- # followed by a comma, linebreak, or string end
- | ####### OR #######
- ([^'.$d.'\n]*+)(?:['.$d.'\n]|$)
- # match anything thats not a comma or linebreak zero to infinity
- # times (possesive), then match either a comma or a linebreak or
- # string end
- )/x',
- $str, $ms, PREG_SET_ORDER
- ) === FALSE ) return FALSE;
- // Initialize vars, $r will hold our return data, $i will track which line we're on
- $r = array(); $i = 0;
- // Loop through results
- foreach( $ms as $m ) {
- // If the first group of matches is empty, the cell has no quotes
- if( empty($m[1]) )
- // Put the CRLF back in if needed
- $r[$i][] = ($crlf == TRUE) ? str_replace("\n", "\r\n", $m[2]) : $m[2];
- else {
- // The cell was quoted, so we want to convert any "" back to " and
- // any LF back to CRLF, if needed
- $r[$i][] = ($crlf == TRUE) ?
- str_replace(
- array("\n", $e.$e),
- array("\r\n", $e),
- $m[1]) :
- str_replace($e.$e, $e, $m[1]);
- }
- // If the raw match doesn't have a delimiter, it must be the last in the
- // row, so we increment our line count.
- if( substr($m[0], -1) != $d )
- $i++;
- }
- // An empty array will exist due to $ being a zero-length match, so remove it
- array_pop( $r );
- return $r;
- }