Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.
How to use
Install via yarn add locutus and import:
import { str_getcsv } from 'locutus/php/strings/str_getcsv'.
Or with CommonJS: const { str_getcsv } = require('locutus/php/strings/str_getcsv')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
/* // 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 = 0 let inpLen = 0 constoutput: string[] = [] const _backwards = function (str: string): string { // 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: string): string { // preg_quote() returnString(str).replace(/([\\.+*?[^\]$(){}=!<>|:])/g, '\\$1') }
/* // 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 = 0 let inpLen = 0 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') }
// PHP behavior may differ by including whitespace even outside of the enclosure const entries = _backwards(trimmedInput) .split(newRegExp(pqEnc + '\\s*' + _pq(delimiterValue) + '\\s*' + pqEnc + '(?!' + pqEsc + ')', 'g')) .reverse()
for (i = 0, inpLen = entries.length; i < inpLen; i++) { output.push(_backwards(entries[i] ?? '').replace(newRegExp(pqEsc + pqEnc, 'g'), enclosureValue)) }
return output }
Improve this function
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.