简体   繁体   中英

How can i convert this string to array?

I am stucked with converting the string of specific format to array. Spliting the string using explode doesn't seems to be the right approach and i am not so good with regular expressions. So my question is how can i convert the following string to array?

Current format of the string

maxWidth: 800,
openEffect: elastic,    
closeEffect: elastic,
helpers : {
       title : {
             type: outside
           },
       thumbs : {
              width  : 50,
              height : 50
            }
      }

Desired Array

array(
  'maxWidth' => 800,
  'openEffect' => 'elastic',
  'closeEffect' => 'elastic',
  'helpers' => array(
               'title' => array('type' => 'outside'),
               'thumbs' => array('width' => 50, 'height' => 50)
             )
)

Any help would be greatly appreciated.

EDIT BASED ON RESPONSES:

The string looks like a JSON but it is not a JSON. Its just a string input from user in that format. The input will be from normal user so i want to keep it simple. There is minimum chance that the normal user will enter a valid JSON.

The string in your example is almost a valid JSON (JavaScript Object Notation) structure!

Here's what your string would look like as valid JSON

 {
    "maxWidth": 800,
    "openEffect": "elastic",
    "closeEffect": "elastic",
    "helpers": {
        "title": {
            "type": "outside"
        },
        "thumbs": {
            "width": 50,
            "height": 50
        }
    }
}

So our approach (as suggested by @WiseGuy) would be to first inject a few characters with preg_replace to Turn your string init into valid JSON:

$str = preg_replace('/\b/' , '"' , $str);
$str = '{'  . $str . '}';

The regex above is using the Word Boundaries anchor to add quotation marks around all words. Then we wrap the whole thing in curly braces and voilà, we've got a x-language compatible object format.

We can now use a standard function to produce our object:

$objUserConfig = json_decode($str, true);

A good beginners tutorial on JSON here: http://code.drewwilson.com/entry/an-introduction-to-json

Use a linter tool such as http://jsonlint.com/ to validate JSON. I used it to debug your example and convert it into proper JSON for my example.

Your input string looks like a json format. PHP has json_decode() to convert json string to object.

To convert to array, use below code:

json_decode($jsonStr, true);

Refer: http://php.net/manual/en/function.json-decode.php

Edit: I know you are showing the printed output of an array and
not a static representation of a php declared array. This is just an
example of how you might convert it into something that can be parsed
into that array. If php has that ability to do so dynamically (I don't know).

Convert the file, read in $str

In this order, do regex on $str.
Each is global flag.

(?i)([az]+) to '$1'
(?i)(?<=[az]')\\s*:(?=\\s*[^{\\s]) to =>
(?i)(?<=[az]')\\s*:\\s*{ to => array(
} to )

Finally, $newstr = "array(\\n$str\\n)"
However, something like this that can be read by a php parser as a static
array. How it gets dynamically interpreted into vars I don't know.
Perl can do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM