PHP正则表达式解析[]之间的数据 [英] PHP regex parse data between [ ]
问题描述
我有一个字符串,我想将其转换为有效的JSON,然后对其进行json_decode. 字符串如下所示:
I've a string that I want to convert to a valid JSON and then json_decode it. Here's what the string looks like:
[
['test', 'lol'],
['test2', 'lol2']
]
[
['test32', 'loDl'],
['test32', 'loDl2']
]
[
['tes23t', 'loDEl'],
['testDE2', 'lolDE2']
]
我只想获取每个之间的数据
I want to get only the data between each
[
]
所以结果可能是:
['test', 'lol'],
['test2', 'lol2'],
['test32', 'loDl'],
['test32', 'loDl2'],
['tes23t', 'loDEl'],
['testDE2', 'lolDE2']
所以我认为我需要使用regex
和preg_split
,这是我所做的:
so I think that I need to use regex
and preg_split
, here's what I did:
$jsons = preg_split('/\]\s*(?=\[)/', $data, null);
$jsond = "";
foreach ($jsons as $json) {
$json .= "";
$jsond .= $json;
}
return $jsond;
但是它不起作用,我仍然无法获得每个[ ]
之间的数据
我该怎么做?
提前感谢
but It's not working, I still can't have the data beetween each [ ]
How can I do that ?
Thanks in advance
PS:这是真实的完整字符串 https://paste.ee/r/RN7rK
推荐答案
指向的字符串具有每行有效的JSON.但是,所有这些行在一起并不代表一个有效的JSON.
The string you pointed to has valid JSON on each line. However, all the lines together do not represent one valid JSON.
我建议以最小的方式处理数据,以使用简单的正则表达式使整个文本JSON.如果原始数据位于 $ data 中,则按如下所示创建JSON:
I propose to manipulate the data in a minimal way to make the whole text JSON with a simple regular expression. If the original data is in $data, then create the JSON as follows:
$json = preg_replace('/(\])\](\R)\[/', '$1,$2', $data);
这将删除行尾的右括号,以及下一行开头的左括号.而是插入一个逗号.结果将是有效的JSON,因为开始时的右括号现在与最后一个右括号匹配.
This will remove both the closing bracket at the end of a line, and the opening one at the start of the next line. Instead a comma is inserted. The result will be valid JSON, as the opening bracket right at the start now matches with the very final closing bracket.
我从您的数据中提取了一些代表性的文字:
I took some representative text from your data:
$data = '[["s","13","shelves_norja","49500","0","1","1","#ffffff,#F7EBBC","Beige Bookcase","For nic naks and books.","","5","true","-1","false","","1","true","0","0","0","false"],["s","117","table_plasto_round*9","45508","0","2","2","#ffffff,#533e10","Round Dining Table","Hip plastic furniture","","-1","false","-1","false","","1","false","0","0","0","false"]]
[["s","118","table_plasto_square*9","45508","0","1","1","#ffffff,#533e10","Occasional Table","Hip plastic furniture","","-1","false","-1","false","","1","false","0","0","0","false"],["s","119","chair_plasto*9","45508","0","1","1","#ffffff,#533e10,#ffffff,#533e10","Chair","Hip plastic furniture","","-1","false","-1","false","","1","false","0","1","0","false"],["s","120","carpet_standard*6","48082","0","3","5","#777777","Floor Rug","Available in a variety of colors","","105","true","-1","false","","1","true","1","0","0","false"],["s","121","chair_plasty*1","45508","0","1","1","#ffffff,#8EB5D1,#ffffff,#8EB5D1","Plastic Pod Chair","Hip plastic furniture","","-1","false","-1","false","","1","false","0","1","0","false"]]';
它只有两行,以限制数据.现在,上面的代码产生了此结果,并进行了漂亮的打印:
It just has two lines, to limit the data a bit. Now the above code produces this result, pretty printed:
[
[
"s",
"13",
"shelves_norja",
"49500",
"0",
"1",
"1",
"#ffffff,#F7EBBC",
"Beige Bookcase",
"For nic naks and books.",
"",
"5",
"true",
"-1",
"false",
"",
"1",
"true",
"0",
"0",
"0",
"false"
],
[
"s",
"117",
"table_plasto_round*9",
"45508",
"0",
"2",
"2",
"#ffffff,#533e10",
"Round Dining Table",
"Hip plastic furniture",
"",
"-1",
"false",
"-1",
"false",
"",
"1",
"false",
"0",
"0",
"0",
"false"
],
[
"s",
"118",
"table_plasto_square*9",
"45508",
"0",
"1",
"1",
"#ffffff,#533e10",
"Occasional Table",
"Hip plastic furniture",
"",
"-1",
"false",
"-1",
"false",
"",
"1",
"false",
"0",
"0",
"0",
"false"
],
[
"s",
"119",
"chair_plasto*9",
"45508",
"0",
"1",
"1",
"#ffffff,#533e10,#ffffff,#533e10",
"Chair",
"Hip plastic furniture",
"",
"-1",
"false",
"-1",
"false",
"",
"1",
"false",
"0",
"1",
"0",
"false"
],
[
"s",
"120",
"carpet_standard*6",
"48082",
"0",
"3",
"5",
"#777777",
"Floor Rug",
"Available in a variety of colors",
"",
"105",
"true",
"-1",
"false",
"",
"1",
"true",
"1",
"0",
"0",
"false"
],
[
"s",
"121",
"chair_plasty*1",
"45508",
"0",
"1",
"1",
"#ffffff,#8EB5D1,#ffffff,#8EB5D1",
"Plastic Pod Chair",
"Hip plastic furniture",
"",
"-1",
"false",
"-1",
"false",
"",
"1",
"false",
"0",
"1",
"0",
"false"
]
]
这篇关于PHP正则表达式解析[]之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!