PHP's str_getcsv in JavaScript

Here’s what our current JavaScript equivalent to PHP's str_getcsv looks like.

module.exports = functionstr_getcsv (input, delimiter, enclosure, escape) { // eslint-disable-line camelcase
// discuss at: https://locutus.io/php/str_getcsv/
// original by: Brett Zamir (https://brett-zamir.me)
// example 1: str_getcsv('"abc","def","ghi"')
// returns 1: ['abc', 'def', 'ghi']
// example 2: str_getcsv('"row2""cell1","row2cell2","row2cell3"', null, null, '"')
// returns 2: ['row2"cell1', 'row2cell2', 'row2cell3']
/*
// These test cases allowing for missing delimiters are not currently supported
str_getcsv('"row2""cell1",row2cell2,row2cell3', null, null, '"');
['row2"cell1', 'row2cell2', 'row2cell3']
str_getcsv('row1cell1,"row1,cell2",row1cell3', null, null, '"');
['row1cell1', 'row1,cell2', 'row1cell3']
str_getcsv('"row2""cell1",row2cell2,"row2""""cell3"');
['row2"cell1', 'row2cell2', 'row2""cell3']
str_getcsv('row1cell1,"row1,cell2","row1"",""cell3"', null, null, '"');
['row1cell1', 'row1,cell2', 'row1","cell3'];
Should also test newlines within
*/
let i
let inpLen
const output = []
const _backwards = function (str) {
// We need to go backwards to simulate negative look-behind (don't split on
// an escaped enclosure even if followed by the delimiter and another enclosure mark)
return str.split('').reverse().join('')
}
const _pq = function (str) {
// preg_quote()
returnString(str).replace(/([\\.+*?[^\]$(){}=!<>|:])/g, '\\$1')
}
delimiter = delimiter || ','
enclosure = enclosure || '"'
escape = escape || '\\'
const pqEnc = _pq(enclosure)
const pqEsc = _pq(escape)
input = input
.replace(newRegExp('^\\s*' + pqEnc), '')
.replace(newRegExp(pqEnc + '\\s*$'), '')
// PHP behavior may differ by including whitespace even outside of the enclosure
input = _backwards(input)
.split(newRegExp(pqEnc + '\\s*' + _pq(delimiter) + '\\s*' + pqEnc + '(?!' + pqEsc + ')', 'g'))
.reverse()
for (i = 0, inpLen = input.length; i < inpLen; i++) {
output.push(_backwards(input[i])
.replace(newRegExp(pqEsc + pqEnc, 'g'), enclosure))
}
return output
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/strings/str_getcsv'). You could also require the strings module in full so that you could access strings.str_getcsv instead.

If you intend to target the browser, you can then use a module bundler such as Parcel, webpack, Browserify, or rollup.js. This can be important because Locutus allows modern JavaScript in the source files, meaning it may not work in all browsers without a build/transpile step. Locutus does transpile all functions to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1str_getcsv('"abc","def","ghi"')['abc', 'def', 'ghi']
2str_getcsv('"row2""cell1","row2cell2","row2cell3"', null, null, '"')['row2"cell1', 'row2cell2', 'row2cell3']

« More PHP strings functions


Star