PHP's str_getcsv in JavaScript

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module.exports = function str_getcsv (input, delimiter, enclosure, escape) { // eslint-disable-line camelcase
// discuss at: http://locutus.io/php/str_getcsv/
// original by: Brett Zamir (http://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
*/


var i
var inpLen
var output = []
var _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('')
}
var _pq = function (str) {
// preg_quote()
return String(str).replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}=!<>\|:])/g, '\\$1')
}

delimiter = delimiter || ','
enclosure = enclosure || '"'
escape = escape || '\\'
var pqEnc = _pq(enclosure)
var pqEsc = _pq(escape)

input = input
.replace(new RegExp('^\\s*' + pqEnc), '')
.replace(new RegExp(pqEnc + '\\s*$'), '')

// PHP behavior may differ by including whitespace even outside of the enclosure
input = _backwards(input)
.split(new RegExp(pqEnc + '\\s*' + _pq(delimiter) + '\\s*' + pqEnc + '(?!' + pqEsc + ')', 'g'))
.reverse()

for (i = 0, inpLen = input.length; i < inpLen; i++) {
output.push(_backwards(input[i])
.replace(new RegExp(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 Browserify, webpack or rollup.js.

ES5/ES6

This function targets ES5, but as of Locutus 2.0.2 we also support ES6 functions. Locutus transpiles 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