par
Saian » 14 juin 2019, 11:26
Je t'ai commenté tout le code et j'ai ajouté une gestion d'erreur si la chaine est mal formatée pour le json_decode. Ainsi y a moyen de déboguer la fonction pour la corriger.
// input string : (a,b,(c,d),((e,f),g),h)
// json string : ["a","b",["c","d"],[["e","f"],"g"],"h"]
function stringToArray($string) {
/** escape some chars */
/* escape brackets with :ob: for open bracket and :cb: for close bracket */
$string = str_replace(array('[', ']'), array(':ob:', ':cb:'), $string);
/* look for strings like 'string' and escape parentheses with :op: for open parenthesis and :cp: for close parenthesis */
$string = preg_replace_callback("`'(.*?)'`", function ($matches) {
return "'".str_replace(array('(', ')'), array(':op:', ':cp:'), $matches[1])."'";
}, $string);
/** jsonify string */
/* replace all strings between , ( ) by "string" */
$string = preg_replace('`([^,\(\)]+)`', '"$1"', $string);
/* replace parentheses by brackets */
$string = str_replace(array('(', ')'), array('[', ']'), $string);
/* convert json string to php array */
$array = json_decode($string);
/* error, bad json string format */
if (!is_array($array)) {
echo "Something went wrong, the string <b>$string</b> is not well formatted for json_decode!<br/>";
exit();
}
/* restore escaped characters by their original character and return the array */
return restoreEscaped($array);
}
function restoreEscaped($array)
{
/* explore the array recursively and restore the escaped characters when the entry is not an array */
return array_map(function ($entry) {
return is_array($entry)
? restoreEscaped($entry)
: str_replace(
array(':ob:', ':cb:', ':op:', ':cp:'),
array('[', ']', '(', ')'),
$entry);
}, $array);
}
function arrayToString($array)
{
/* escape the brackets in the array */
$array = escapeBrackets($array);
/* convert php array to json string */
$string = json_encode($array);
/* replace brackets, double quotes and escaped brackets to match input string format */
$string = str_replace(array('[', ']', '"', ':ob:', ':cb:'), array('(', ')', '', '[', ']'), $string);
return $string;
}
function escapeBrackets($array) {
/* explore the array recursively and escape the brackets when entry is not an array */
return array_map(function ($entry) {
return is_array($entry)
? escapeBrackets($entry)
: str_replace(
array('[', ']'),
array(':ob:', ':cb:'),
$entry);
}, $array);
}
Je t'ai commenté tout le code et j'ai ajouté une gestion d'erreur si la chaine est mal formatée pour le json_decode. Ainsi y a moyen de déboguer la fonction pour la corriger.
[PHP]// input string : (a,b,(c,d),((e,f),g),h)
// json string : ["a","b",["c","d"],[["e","f"],"g"],"h"]
function stringToArray($string) {
/** escape some chars */
/* escape brackets with :ob: for open bracket and :cb: for close bracket */
$string = str_replace(array('[', ']'), array(':ob:', ':cb:'), $string);
/* look for strings like 'string' and escape parentheses with :op: for open parenthesis and :cp: for close parenthesis */
$string = preg_replace_callback("`'(.*?)'`", function ($matches) {
return "'".str_replace(array('(', ')'), array(':op:', ':cp:'), $matches[1])."'";
}, $string);
/** jsonify string */
/* replace all strings between , ( ) by "string" */
$string = preg_replace('`([^,\(\)]+)`', '"$1"', $string);
/* replace parentheses by brackets */
$string = str_replace(array('(', ')'), array('[', ']'), $string);
/* convert json string to php array */
$array = json_decode($string);
/* error, bad json string format */
if (!is_array($array)) {
echo "Something went wrong, the string <b>$string</b> is not well formatted for json_decode!<br/>";
exit();
}
/* restore escaped characters by their original character and return the array */
return restoreEscaped($array);
}
function restoreEscaped($array)
{
/* explore the array recursively and restore the escaped characters when the entry is not an array */
return array_map(function ($entry) {
return is_array($entry)
? restoreEscaped($entry)
: str_replace(
array(':ob:', ':cb:', ':op:', ':cp:'),
array('[', ']', '(', ')'),
$entry);
}, $array);
}
function arrayToString($array)
{
/* escape the brackets in the array */
$array = escapeBrackets($array);
/* convert php array to json string */
$string = json_encode($array);
/* replace brackets, double quotes and escaped brackets to match input string format */
$string = str_replace(array('[', ']', '"', ':ob:', ':cb:'), array('(', ')', '', '[', ']'), $string);
return $string;
}
function escapeBrackets($array) {
/* explore the array recursively and escape the brackets when entry is not an array */
return array_map(function ($entry) {
return is_array($entry)
? escapeBrackets($entry)
: str_replace(
array('[', ']'),
array(':ob:', ':cb:'),
$entry);
}, $array);
}[/PHP]