| : | Javascript Index | : | PHP | : | MySQL | : |
| : | Source | : | : | Explanation | : | : | Example | : | : | Todo | : | : | Feedback | : |
A collection of functions that will enable you to convert a serialized php-array into a javascript array
Added: 2006-04-10 09:14:12
/******
* You may use and/or modify this script as long as you:
* 1. Keep my name & webpage mentioned
* 2. Don't use it for commercial purposes
*
* If you want to use this script without complying to the rules above, please contact me first at: marty@excudo.net
*
* Author: Martijn Korse
* Website: http://devshed.excudo.net
*
* Date: 2006-04-10 09:14:12
***/
function phpArr2jsArr(serString)
{
var firstAndLast = getFirstAndLast(serString);
var subbed = serString.substring(firstAndLast[0], firstAndLast[1]);
var subbedLength = getArraySize(serString.substring(0, firstAndLast[0]));
var arr = createArray(subbed, subbedLength, 0);
return arr;
}
function createArray(str, lengte, counter)
{
counter++;
// initializing the array (object)
var serArr = new Object();
// i will hold the position of where we are in the string
var i = 0;
// number will keep track of how many elements we've parsed. It can never exceed the variable lengte
var number = 0;
while (number < lengte)
{
/*****
* Getting the key/index of the current element
***/
// first we're gonna determine the length of the key.
// this is represented by a number in the string.
// offset is gonna contain the first position of that number and j will hold the last position after the while loop
var loop_i = 0; // loop_i is gonna keep track of how many characters we advance this element
var offset = i+2;
var j = offset;
var continu = true;
while (continu)
{
// currentChar is the current character we're evaluating
currentChar = str.charAt(j);
// if the currentChar is not a number, we break. else, we just increase j so we can evaluate the next
if (currentChar != parseInt(currentChar))
{
break;
}
else
j++;
}
loop_i = loop_i + 2 + (j- offset); // 2 is for i: or s:
// j will now hold the position of the last digit of the number
// we're dealing with a key/string that is represented as a String
// we now can determine the length and then extract the name of the key/index
if (str.charAt(i) == 's')
{
loop_i = loop_i + 1; // the : that precedes the string "value"
// the numerical representation of the length (for example with 'index' this would be 5
var keyLength = str.substring(offset, j);
// thisKey is now gonna get the name of the key/index (in our example : index)
var thisKey = str.substr((j+2), parseInt(keyLength));
// we log the intervals for debugging purposes
var keyIntervals = (j+2)+','+keyLength;
// after having determined the key/index we're gonna determine the corresponding value
// instead of i we're gonna use the variable nextI.
// here we give it the right position
var nextI = j+2+parseInt(keyLength)+2;
loop_i = loop_i + 2 + thisKey.length; // 2 is for the double quotes around the string
}
// we're dealing with a key/string that is represented as an Integer
// this means that the number we just extracted is also the name of the key/index
else if (str.charAt(i) == 'i')
{
var keyLength = str.substring(offset, j);
var thisKey = keyLength;
var keyIntervals = offset+','+j;
var nextI = i + 1 + keyLength.length + 2;
}
else
{
// this typically happens when encoding types are mixed.
alert('!!!str.charAt(i) is geen i maar ook geen s!!! ('+str.charAt(i)+')');
}
loop_i = loop_i + 1; // the ; that ends the key
/*****
* Getting the value of the current element
***/
var offset = nextI+2;
var j = offset;
var continu = true;
while (continu)
{
var currentChar = str.charAt(j);
if (currentChar != parseInt(currentChar))
{
break;
}
else
j++;
}
loop_i = loop_i + 2 + (j- offset); // 2 is for a: or s:
loop_i = loop_i + 1; // 1 for the : after that
var valLength = str.substring(offset, j);
// we're dealing with an array
if (str.charAt(nextI) == 'a')
{
var stringFromArray = str.substr(nextI);
var tempFirstAndLast = getFirstAndLast(stringFromArray);
var newArrayString = stringFromArray.substring(tempFirstAndLast[0], tempFirstAndLast[1]);
var thisValue = createArray(newArrayString, valLength, counter);
var valIntervals = tempFirstAndLast[0]+','+tempFirstAndLast[1];
loop_i = loop_i + 2 + newArrayString.length; // 2 is for the {}
}
// it's a normal string
else
{
var thisValue = str.substr((j+2), parseInt(valLength));
var valIntervals = (j+2)+','+valLength;
loop_i = loop_i + thisValue.length + 2 + 1; // 2 is the double quotes again and 1 the ending ;
}
i = i + loop_i;
number++;
serArr[thisKey] = thisValue;
}
return serArr;
}
function getFirstAndLast(str)
{
var lengte = str.length;
var first = str.search(/{/);
var i = first;
var whenZero = 1;
while (++i < lengte && whenZero > 0)
{
if (str.charAt(i) == '}')
{
whenZero--;
}
else if (str.charAt(i) == '{')
{
whenZero++;
}
if (whenZero == 0)
{
var last = i;
}
}
var arr = [];
arr[0] = first+1;
arr[1] = last;
return arr;
}
function getArraySize(str)
{
j = 2;
var continu = true;
while (continu)
{
// currentChar is the current character we're evaluating
currentChar = str.charAt(j);
// if the currentChar is not a number, we break. else, we just increase j so we can evaluate the next
if (currentChar != parseInt(currentChar))
{
break;
}
else
j++;
}
return str.substring(2, j);
}
// this function is not specifically relevant for the serial-killer, but helpfull for debugging purposes
function showObj(arr, indent)
{
var output = '';
for (var i in arr)
{
output = output + '<div style="padding-left: '+indent+'px;">'+i;
if (typeof arr[i] == 'string')
{
output = output + ' => '+arr[i]+'</div>';
}
else
{
output = output + ' => Array()</div>';
output = output + showObj(arr[i], (indent + 15))
}
}
return output;
}