phpjs
Version:
187 lines (168 loc) • 5.91 kB
Markdown
---
layout: page
title: "JavaScript parse_str function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/parse_str:484
- /functions/view/parse_str
- /functions/view/484
- /functions/parse_str:484
- /functions/484
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's parse_str
{% codeblock strings/parse_str.js lang:js https://raw.github.com/kvz/phpjs/master/functions/strings/parse_str.js raw on github %}
function parse_str (str, array) {
// From: http://phpjs.org/functions
// + original by: Cagri Ekin
// + improved by: Michael White (http://getsprink.com)
// + tweaked by: Jack
// + bugfixed by: Onno Marsman
// + reimplemented by: stag019
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: stag019
// + input by: Dreamer
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: MIO_KODUKI (http://mio-koduki.blogspot.com/)
// + input by: Zaide (http://zaidesthings.com/)
// + input by: David Pesta (http://davidpesta.com/)
// + input by: jeicquest
// + improved by: Brett Zamir (http://brett-zamir.me)
// % note 1: When no argument is specified, will put variables in global scope.
// % note 1: When a particular argument has been passed, and the returned value is different parse_str of PHP. For example, a=b=c&d====c
// % test: skip
// * example 1: var arr = {};
// * example 1: parse_str('first=foo&second=bar', arr);
// * example 1: arr
// * returns 1: { first: 'foo', second: 'bar' }
// * example 2: var arr = {};
// * example 2: parse_str('str_a=Jack+and+Jill+didn%27t+see+the+well.', arr);
// * example 2: arr
// * returns 2: { str_a: "Jack and Jill didn't see the well." }
// * example 3: var abc = {3:'a'};
// * example 3: parse_str('abc[a][b]["c"]=def&abc[q]=t+5');
// * returns 3: {"3":"a","a":{"b":{"c":"def"}},"q":"t 5"}
var strArr = String(str).replace(/^&/, '').replace(/&$/, '').split('&'),
sal = strArr.length,
i, j, ct, p, lastObj, obj, lastIter, undef, chr, tmp, key, value,
postLeftBracketPos, keys, keysLen,
fixStr = function (str) {
return decodeURIComponent(str.replace(/\+/g, '%20'));
};
if (!array) {
array = this.window;
}
for (i = 0; i < sal; i++) {
tmp = strArr[i].split('=');
key = fixStr(tmp[0]);
value = (tmp.length < 2) ? '' : fixStr(tmp[1]);
while (key.charAt(0) === ' ') {
key = key.slice(1);
}
if (key.indexOf('\x00') > -1) {
key = key.slice(0, key.indexOf('\x00'));
}
if (key && key.charAt(0) !== '[') {
keys = [];
postLeftBracketPos = 0;
for (j = 0; j < key.length; j++) {
if (key.charAt(j) === '[' && !postLeftBracketPos) {
postLeftBracketPos = j + 1;
}
else if (key.charAt(j) === ']') {
if (postLeftBracketPos) {
if (!keys.length) {
keys.push(key.slice(0, postLeftBracketPos - 1));
}
keys.push(key.substr(postLeftBracketPos, j - postLeftBracketPos));
postLeftBracketPos = 0;
if (key.charAt(j + 1) !== '[') {
break;
}
}
}
}
if (!keys.length) {
keys = [key];
}
for (j = 0; j < keys[0].length; j++) {
chr = keys[0].charAt(j);
if (chr === ' ' || chr === '.' || chr === '[') {
keys[0] = keys[0].substr(0, j) + '_' + keys[0].substr(j + 1);
}
if (chr === '[') {
break;
}
}
obj = array;
for (j = 0, keysLen = keys.length; j < keysLen; j++) {
key = keys[j].replace(/^['"]/, '').replace(/['"]$/, '');
lastIter = j !== keys.length - 1;
lastObj = obj;
if ((key !== '' && key !== ' ') || j === 0) {
if (obj[key] === undef) {
obj[key] = {};
}
obj = obj[key];
}
else { // To insert new dimension
ct = -1;
for (p in obj) {
if (obj.hasOwnProperty(p)) {
if (+p > ct && p.match(/^\d+$/g)) {
ct = +p;
}
}
}
key = ct + 1;
}
}
lastObj[key] = value;
}
}
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/strings/parse_str.js)
Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
the closest match to this hashtable-like data structure.
Please also note that php.js offers community built functions and goes by the
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
functions that are far from perfect, in the hopes to spark better contributions.
Do you have one? Then please just:
- [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/strings/parse_str.js)
### Example 1
This code
{% codeblock lang:js example %}
var arr = {};
parse_str('first=foo&second=bar', arr);
arr
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
{ first: 'foo', second: 'bar' }
{% endcodeblock %}
### Example 2
This code
{% codeblock lang:js example %}
var arr = {};
parse_str('str_a=Jack+and+Jill+didn%27t+see+the+well.', arr);
arr
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
{ str_a: "Jack and Jill didn't see the well." }
{% endcodeblock %}
### Example 3
This code
{% codeblock lang:js example %}
var abc = {3:'a'};
parse_str('abc[a][b]["c"]=def&abc[q]=t+5');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
{"3":"a","a":{"b":{"c":"def"}},"q":"t 5"}
{% endcodeblock %}
### Other PHP functions in the strings extension
{% render_partial _includes/custom/strings.html %}