将字符串解析为数组 {{navigation({"class": "navigation", "id": "navigation"})}} 第 2 部分

发布于 2024-10-19 17:11:05 字数 518 浏览 4 评论 0 原文

让我们简化问题:

我需要的只是用括号之间的逗号来explode()字符串。问题是用逗号选择的元素本身可以有逗号,因此简单的爆炸不起作用。我不是问如何解码 JSON。

参数的数量,它们的类型总是不同的,例如

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

假设我有这部分代码:

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

任何人都可以建议一个正则表达式(或替代方法)来获取所有逗号分隔的条目(作为字符串)?问题是变量本身可以包含逗号。因此,我认为这需要递归。

预期结果将是一个包含以下条目的数组:

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

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

亣腦蒛氧 2024-10-26 17:11:05

经过几个小时的工作,我发现 PHP 无法解析包含 NULL(大写 null)的 JSON 字符串。这就是导致以下代码无法工作的原因。不过,只需将 NULL 替换为 null 就可以解决问题。

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

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"]')
谈场末日恋爱 2024-10-26 17:11:05

这是有效的:

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

结果:

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

不完全是递归正则表达式,而是前瞻断言。条件是逗号后面不能跟},或者应该跟{。

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 {.

寻找我们的幸福 2024-10-26 17:11:05

看起来像 json.您应该使用 json_decode,它将创建一个数组,然后您可以循环遍历该数组以获取键/值。

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.

爱她像谁 2024-10-26 17:11:05

黑客警报!

  1. 将 {} 括在单引号中,以便您的 {"JSON": "data"} 变为 '{"JSON": "data"}'

  2. 使用 str_getcsv() 解析字符串(现在是 CSV)

  3. 遍历结果数组并从 {} 中去掉单引号

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 {}

心欲静而疯不止 2024-10-26 17:11:05

在 [ 和 ] 之间添加该代码,并使用 json_decode()

它应该返回一个关联数组

您应该使用 < code>[] 表示隐式索引,{} 表示显式索引

示例

[] : ['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)


您不能将隐式/显式索引混合到同一结构中,但您可以创建一个包含不同值的数组 ([]),如上面的示例所示。

$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'

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'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文