我在用PHP写作。我有这个字符串:
'[["<span class=\"highlighted\" style=\"background-color: yellow;\"></span>","This is example filler text","3:1",4,37],["<span class=\"highlighted\" style=\"background-color: yellow;\"></span>"," Filler text is an example usage of a highlighter JQuery plugin utilized for a drupal module. \n ","5:1",5,85],["<span class=\"highlighted\" style=\"background-color: yellow;\"></span>","Highlights are serialized to when \"save\" button is pressed. \n ","5:3",0,108],["<span class=\"highlighted\" style=\"background-color: yellow;\"></span>","The serialized J","5:5",0,16]]'
上面的字符串只是一个JavaScript样式的数组数组的字符串。我希望能够做到的是将其转换为实际的PHP数组。但我也希望能够将PHP数组转换回这个确切的字符串。
最终结果与执行这些步骤的最终结果类似(我认为):
1)删除第一个和最后一个单引号
2)将每个'['字符替换为'array('和''''字符')'
因此,我最终会得到一个有效的PHP数组。而且我也不确定我需要采取什么措施来扭转这一过程。
答案 0 :(得分:3)
您可以使用json_encode
和json_decode
:
$php_array = json_decode('[["<span class=\"highlighted\" style=\"background-color: yellow;\"></span>","This is example filler text","3:1",4,37],["<span class=\"highlighted\" style=\"background-color: yellow;\"></span>"," Filler text is an example usage of a highlighter JQuery plugin utilized for a drupal module. \n ","5:1",5,85],["<span class=\"highlighted\" style=\"background-color: yellow;\"></span>","Highlights are serialized to when \"save\" button is pressed. \n ","5:3",0,108],["<span class=\"highlighted\" style=\"background-color: yellow;\"></span>","The serialized J","5:5",0,16]]', true);
$js_array = json_encode($php_array);
答案 1 :(得分:0)
试试这个 的Javascript
var string = '[["<span class=\"highlighted\" style=\"background-color: yellow;\"></span>","This is example filler text","3:1",4,37],["<span class=\"highlighted\" style=\"background-color: yellow;\"></span>"," Filler text is an example usage of a highlighter JQuery plugin utilized for a drupal module. \n ","5:1",5,85],["<span class=\"highlighted\" style=\"background-color: yellow;\"></span>","Highlights are serialized to when \"save\" button is pressed. \n ","5:3",0,108],["<span class=\"highlighted\" style=\"background-color: yellow;\"></span>","The serialized J","5:5",0,16]]';
$.post("phpfile.php",{data:string},function(data){ var newVar = data.var; },"json");
PHP
<?php $data = json_decode($_POST["data"]); /*CODE*/ echo json_encode(array("var" => $data)); ?>