0

I am writing in PHP. I have this 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]]'

The above string, is simply a string of a JavaScript style array of arrays. What I would like to be able to do, is convert this to an actual PHP array. But I would also like to be able to convert that PHP array back to this exact string.

The end result would be similar to the end result of doing these steps (I think):

1) remove the first and last single quotation marks

2) replace each '[' character with 'array(' and each ']' character with ')'

Thus I would end up with a valid PHP array of arrays. And I am also unsure of what I would need to do to reverse this process.

6
  • 2
    Looks like JSON to me. PHP has built-in functions to pare JSON. Commented Jul 12, 2014 at 15:17
  • I agree with @FelixKling. Have you looked into using json_decode? Commented Jul 12, 2014 at 15:22
  • @FelixKling Looking into it now! I'm confused at you saying this looks like JSON. Is an array of arrays of strings JSON in PHP? I thought JSON had { "syntax": "like" } <this. Commented Jul 12, 2014 at 15:28
  • JSON is just a JavaScript representation of object data, which maps quite nicely to either stdClass PHP objects, associative arrays, or indexed arrays in PHP. Commented Jul 12, 2014 at 16:22
  • Nope, JSON can also encode lists, which looks like [v, v, v]. The string contains JSON. Commented Jul 12, 2014 at 16:52

2 Answers 2

3

You can use json_encode and 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);
Sign up to request clarification or add additional context in comments.

2 Comments

thank's for the reply, Question, will $js_array essentially contain the same exact string value that it had originally?
Yes and no... the json_encode will escape /, so </span> will become <\/span>, as that is proper JSON. But aside from that yes, they will be exactly the same.
0

Try this 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)); ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.