phparraystext-parsing

Parse a curly braced string containing comma-separated numbers into a flat array


Is there a way to convert a string in the form show below into an array in PHP?

{1,2,3,4} 

The string is stored on a database like this and I do not control the way things are inserted into the database.


Solution

  • Convert it to a valid json array, then json_decode it?

    $data = '{1,2,3,4}';
    $d = json_decode(str_replace(['{','}'], ['[',']'], $data));
    var_dump($d);