ninjascorner
ninjascorner

Reputation: 299

Convert a JSON string with single quotes to an array

This may be asked several times but my case is a bit different.

Let me start from the beginning.

$ck_data = db_select('ckeditor_settings', 'cs')
           ->fields('cs', array('settings'))
           ->condition('name', 'Advanced', '=')
           ->execute()
           ->fetchAssoc();
var_dump($ck_data);

Will give me...

array(1) {
  ["settings"]=>
  string(2144) "a:33:{s:2:"ss";s:1:"2";s:7:"toolbar";s:606:"[
    ['Source'],
    ['Cut','Copy','Paste','PasteText','PasteFromWord','-','SpellChecker','Scayt'],
    ['Undo','Redo','Find','Replace','-','SelectAll'],
    ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar'],
    ['Maximize','ShowBlocks'],
    '/',
    ['Format'],
    ['Bold','Italic','Underline','Strike','-','Subscript','Superscript','-','RemoveFormat'],
    ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
    ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl'],
    ['Link','Unlink','Anchor','Linkit','LinkToNode','LinkToMenu']
]";s:6:"expand";s:1:"t";s:7:"default";s:1:"t";s:11:"show_toggle";s:1:"t";s:7:"uicolor";s:7:"default";s:12:"uicolor_user";s:7:"default";s:5:"width";s:4:"100%";s:4:"lang";s:2:"en";s:9:"auto_lang";s:1:"t";s:18:"language_direction";s:7:"default";s:15:"allowed_content";s:1:"t";s:19:"extraAllowedContent";s:0:"";s:10:"enter_mode";s:1:"p";s:16:"shift_enter_mode";s:2:"br";s:11:"font_format";s:35:"p;div;pre;address;h1;h2;h3;h4;h5;h6";s:17:"custom_formatting";s:1:"f";s:10:"formatting";a:1:{s:25:"custom_formatting_options";a:6:{s:6:"indent";s:6:"indent";s:15:"breakBeforeOpen";s:15:"breakBeforeOpen";s:14:"breakAfterOpen";s:14:"breakAfterOpen";s:15:"breakAfterClose";s:15:"breakAfterClose";s:16:"breakBeforeClose";i:0;s:10:"pre_indent";i:0;}}s:8:"css_mode";s:4:"none";s:8:"css_path";s:0:"";s:9:"css_style";s:5:"theme";s:11:"styles_path";s:0:"";s:11:"filebrowser";s:4:"none";s:17:"filebrowser_image";s:0:"";s:17:"filebrowser_flash";s:0:"";s:13:"UserFilesPath";s:5:"%b%f/";s:21:"UserFilesAbsolutePath";s:7:"%d%b%f/";s:21:"forcePasteAsPlainText";s:1:"t";s:13:"html_entities";s:1:"f";s:17:"scayt_autoStartup";s:1:"t";s:15:"theme_config_js";s:1:"f";s:7:"js_conf";s:0:"";s:11:"loadPlugins";a:1:{s:12:"drupalbreaks";a:5:{s:4:"name";s:12:"drupalbreaks";s:4:"desc";s:51:"Plugin for inserting Drupal teaser and page breaks.";s:4:"path";s:25:"%plugin_dir%drupalbreaks/";s:7:"buttons";a:1:{s:11:"DrupalBreak";a:2:{s:5:"label";s:11:"DrupalBreak";s:4:"icon";s:22:"images/drupalbreak.png";}}s:7:"default";s:1:"t";}}}"
}

Now what I want is to get the value of toolbar.

$ck_settings = unserialize($ck_data['settings']);
$ck_plugins = $ck_settings['toolbar'];
var_dump($ck_plugins);

Will return...

string(606) "[
    ['Source'],
    ['Cut','Copy','Paste','PasteText','PasteFromWord','-','SpellChecker','Scayt'],
    ['Undo','Redo','Find','Replace','-','SelectAll'],
    ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar'],
    ['Maximize','ShowBlocks'],
    '/',
    ['Format'],
    ['Bold','Italic','Underline','Strike','-','Subscript','Superscript','-','RemoveFormat'],
    ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
    ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl'],
    ['Link','Unlink','Anchor','Linkit','LinkToNode','LinkToMenu']
]"

My question now is how can I convert $ck_plugins from string to array?

Upvotes: 0

Views: 151

Answers (2)

mickmackusa
mickmackusa

Reputation: 47874

The json-like string nested in your serialized string merely uses invalidating quotes. If you replace single quotes with double quotes, you can safely call json_decode().

Code: (Demo)

var_export(
    json_decode(
        str_replace(
            "'",
            '"',
            unserialize(
                $ck_data['settings']
            )['toolbar']
        )
    )
);

Upvotes: 0

AbraCadaver
AbraCadaver

Reputation: 78994

Strange way to be storing that, but it is a string that looks like a PHP array definition:

eval("\$ck_plugins = $ck_plugins;");
print_r($ck_plugins);

If you have control over the data storage, you should probably either store the individual entries in a table or store the entire thing serialized or better in JSON.

Upvotes: 2

Related Questions