0

I got this string :

$mce_settings = "{alignleft: [{selector: "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li", styles: {textAlign:"left"}},{selector: "img,table,dl.wp-caption", classes: "alignleft"}],aligncenter: [{selector: "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li", styles: {textAlign:"center"}},{selector: "img,table,dl.wp-caption", classes: "aligncenter"}],alignright: [{selector: "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li", styles: {textAlign:"right"}},{selector: "img,table,dl.wp-caption", classes: "alignright"}],strikethrough: {inline: "del"}}"

I would like to convert it in php array, so I tried this without success :

json_decode( $mce_settings, true ); //return NULL

Someone has got an idea ?

1 Answer 1

2

Your json is malformed (you can see json decode errors using json_last_error(), as explained on the official documentation: json_last_error) due to missing quotes.

Try the following json string:

$mce_settings = '{"alignleft":[{"selector":"p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li","styles":{"textAlign":"left"}},{"selector":"img,table,dl.wp-caption","classes":"alignleft"}],"aligncenter":[{"selector":"p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li","styles":{"textAlign":"center"}},{"selector":"img,table,dl.wp-caption","classes":"aligncenter"}],"alignright":[{"selector":"p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li","styles":{"textAlign":"right"}},{"selector":"img,table,dl.wp-caption","classes":"alignright"}],"strikethrough":{"inline":"del"}}';

var_dump(json_decode( $mce_settings, true ));

This prints:

array(4) {
  ["alignleft"]=>
  array(2) {
    [0]=>
    array(2) {
      ["selector"]=>
      string(38) "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li"
      ["styles"]=>
      array(1) {
        ["textAlign"]=>
        string(4) "left"
      }
    }
    [1]=>
    array(2) {
      ["selector"]=>
      string(23) "img,table,dl.wp-caption"
      ["classes"]=>
      string(9) "alignleft"
    }
  }
  ["aligncenter"]=>
  array(2) {
    [0]=>
    array(2) {
      ["selector"]=>
      string(38) "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li"
      ["styles"]=>
      array(1) {
        ["textAlign"]=>
        string(6) "center"
      }
    }
    [1]=>
    array(2) {
      ["selector"]=>
      string(23) "img,table,dl.wp-caption"
      ["classes"]=>
      string(11) "aligncenter"
    }
  }
  ["alignright"]=>
  array(2) {
    [0]=>
    array(2) {
      ["selector"]=>
      string(38) "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li"
      ["styles"]=>
      array(1) {
        ["textAlign"]=>
        string(5) "right"
      }
    }
    [1]=>
    array(2) {
      ["selector"]=>
      string(23) "img,table,dl.wp-caption"
      ["classes"]=>
      string(10) "alignright"
    }
  }
  ["strikethrough"]=>
  array(1) {
    ["inline"]=>
    string(3) "del"
  }
}
Sign up to request clarification or add additional context in comments.

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.