I try build a php regex that validate this type of input string:
{name:'something name here',type:'',id:''},{name:'other name',type:'small',id:34},{name:'orange',type:'weight',id:28}
etc...
So, it is a list of json that each contain 3 field: name,type,id.Field name is always present, instead type and id can be together empty string ( '' ). Then I can explode it by comma if it has valid format and obtain a array of json string.
How can I do?
UPDATE it isn't a valid json as you can say but I have a input field where user put tags, and I want track a name, type and id of that tags. example: tag1 (has name,type,id), tags2 (has only name), tags3(has name, type,id). So, I think that I can post a string in that format:
{'name':'test','type':'first','id':3},{'name':'other','type':'second','id':45}, etc
But I must validate this string with a regex. I can do
$data = explode(',',$list);
and then I do:
foreach($data as $d){
$tmp = json_decode($d);
if($tmp == false) echo 'error invalid data';
}
json_decodeafter exploding on the comma's won't work: basically, the resulting array will be['{name:test', 'type:first','id:3}','{name:other'.... You're better off usingpreg_split:$separated= preg_split('/(?<=[\}]),/',$string1);. The resulting array will be split in parts like{'name':'test','type':'first','id':3}, which are valid JSON strings, I pointed this out in my answer$parsed = json_decode('['.$string1.']');. Just adding the delimiting brackets manually. This will return a clean, parsed array... and it's faster than using preg_split