Domanda
C'è un modo in cui potrei godere di una funzione decodeValue ()
anche in PHP? Sto pubblicando quei valori codificatiValue in un file PHP e ho bisogno di lavorare con loro in PHP come un array.
Come posso finire con un array PHP o qualcosa dello stato codificato in Ext? Oppure, c'è un altro modo in cui potrei lavorare i valori codificati per poterli leggere facilmente in PHP? Ecco il codice funzione:
decodeValue : function(cookie){
var re = /^(a|n|d|b|s|o)\:(.*)$/;
var matches = re.exec(unescape(cookie));
if(!matches || !matches[1]) return; // non state cookie
var type = matches[1];
var v = matches[2];
switch(type){
case "n":
return parseFloat(v);
case "d":
return new Date(Date.parse(v));
case "b":
return (v == "1");
case "a":
var all = [];
var values = v.split("^");
for(var i = 0, len = values.length; i < len; i++){
all.push(this.decodeValue(values[i]));
}
return all;
case "o":
var all = {};
var values = v.split("^");
for(var i = 0, len = values.length; i < len; i++){
var kv = values[i].split("=");
all[kv[0]] = this.decodeValue(kv[1]);
}
return all;
default:
return v;
}
}
Grazie.
Soluzione
Di seguito è la mia porta a PHP. Ho usato la classe DateTime al posto di Date in quanto è l'equivalente PHP più vicino, ma potresti anche usare strftime () per ottenere un timestamp Unix, o qualunque metodo tu preferisca. Inoltre, per il tipo 'o' restituisco un array anziché un oggetto, digitato dai nomi dei parametri dell'oggetto.
Ecco il codice:
function decodeValue($cookie) {
$cookie = urldecode($cookie);
$re = '/^(a|n|d|b|s|o)\:(.*)$/';
$matches = array();
preg_match($re, $cookie, $matches);
if(!$matches || !$matches[1]) return; // non state cookie
$type = $matches[1];
$v = $matches[2];
switch ($type){
case "n":
return floatval($v);
case "d":
return new DateTime($v);
case "b":
return ($v == "1" ? true : false);
case "a":
$all = array();
$values = explode('^', $v);
$len = count($values);
for ($i = 0; $i < $len; $i++) {
$all.push(decodeValue($values[$i]));
}
return $all;
case "o":
$all = array();
$values = explode('^', $v);
$len = count($values);
for($i = 0; $i < $len; $i++){
$kv = explode('=', $values[$i]);
$all[$kv[0]] = decodeValue($kv[1]);
}
return $all;
default:
return $v;
}
}
Altri suggerimenti
Risolto un bug nel codice. Ora l'array di secondo livello / terzo livello dovrebbe funzionare correttamente.
function decodeValue($cookie) {
$cookie = urldecode($cookie);
$re = '/^(a|n|d|b|s|o)\:(.*)$/';
$matches = array();
preg_match($re, $cookie, $matches);
if(!$matches || !$matches[1]) return $cookie; // non state cookie
$type = $matches[1];
$v = $matches[2];
switch ($type){
case "n":
return floatval($v);
case "d":
return new DateTime($v);
case "b":
return ($v == "1" ? true : false);
case "a":
$all = array();
$values = explode('^', $v);
$len = count($values);
for ($i = 0; $i < $len; $i++) {
$all.array_push(decodeValue($values[$i]));
}
return $all;
case "o":
$all = array();
$values = explode('^', $v);
$len = count($values);
for($i = 0; $i < $len; $i++){
$kv = explode('=', $values[$i],2);
if(count($kv)==1){
$all[] = decodeValue($kv[0]);
}else{
$all[$kv[0]] = decodeValue($kv[1]);
}
}
return $all;
default:
return $v;
}
}
$all.array_push(decodeValue($values[$i]));
deve essere sostituito con
$all[] = decodeValue($values[$i]);