Gajus
Gajus

Reputation: 73968

parsing string into array {{navigation({"class": "navigation", "id": "navigation"})}} part 2

Let simplify the question:

All I need is to explode() string by a comma between brackets. The problem is that elements selected by comma can have a comma in itself, thus simple exploding won't work. I am not asking how to decode JSON.

The number of arguments, their type will always be different, e.g.

('foo')
('bar', NULL)
({"JSON": "data"}, 'test')

Assuming that I have this part of the code:

({"class": "navigation", "id": "navigation"}, NULL, 'bar' /* [..] */)

Can anyone suggest a regex (or alternative method) to get all the comma separated entries (as string)? The problem is that variables can contain commas in itself. Thus, I assume this requires recursion.

Expected result would be an array containing following entries:

{"class": "navigation", "id": "navigation"}
NULL
'bar'

Upvotes: 2

Views: 265

Answers (5)

Sergey
Sergey

Reputation: 1173

Here's something that works:

$str = 'NULL, {"class": "navigation", "id": "navigation"}, NULL, \'bar\'';
var_dump(preg_split( '%(,(?!.*})|,(?=.*{)\s+)%', $str));

Result:

~$ php ./test.php
array(4) {
  [0]=>
  string(4) "NULL"
  [1]=>
  string(44) "{"class": "navigation", "id": "navigation"}"
  [2]=>
  string(5) "NULL"
  [3]=>
  string(6) "'bar'"
}

Not exactly a recursive regex, but rather a look ahead assertion. The condition is that comma should not be followed by }, or it should be followed by {.

Upvotes: 2

w35l3y
w35l3y

Reputation: 8783

Add that code between [ and ], and use json_decode()

It should return an associative array

You should use [] for implicit indexes and {} for explicit indexes

Example:

[] : ['w', null, 123, {'a':1}] <- implicit keys (0, 1, 2, 3)
{} : {'0':'w', '1':null, '2':123, '3':{'a':1}} <- explicit keys (0, 1, 2, 3)

  You can not mix implicit/explicit indexes into the same structure, but you can for example, create an array ([]) containing different values as shown in the examples above.

$code = substr('({"id":"navigation"}, null, "bar")', 1, -1); // removes ^\( and \)$
$result = json_decode('[' . $code . ']');

echo $result[0]['id']; // returns 'navigation'
echo $result[1]; // returns NULL
echo $result[2]; // returns 'bar'

Upvotes: 0

Gajus
Gajus

Reputation: 73968

After many hours of work, I have come across that PHP will fail to parse JSON string containing NULL (uppercase null). That was what caused the following code not to work. However, simply replacing NULL to null solved the problem.

var_dump(json_decode('[{"class": "navigation", "id": "navigation"}, NULL, "bar"]')

Upvotes: 2

beklip
beklip

Reputation: 98

hack alert!

  1. wrap {} into single quotes, so that your {"JSON": "data"} becomes '{"JSON": "data"}'

  2. use str_getcsv() to parse a string (it's a CSV now)

  3. go though resulting array and strip single quotes from {}

Upvotes: 0

Brent Baisley
Brent Baisley

Reputation: 12721

That looks like json. You should use json_decode, which will create an array, then you can loop through the array to get the key/values.

Upvotes: 0

Related Questions