0

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'; 
}
4
  • 3
    This is not valid JSON: in JSON, the name in a name/value pair must be a proper string. Commented Apr 29, 2012 at 20:06
  • using json_decode after 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 using preg_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 Commented Apr 29, 2012 at 22:25
  • Ok you are right. It doesn't work because explode view first comma term not first element. I use now preg_split Commented Apr 29, 2012 at 22:57
  • I also pointed out that preg_split is overkill, especially now that I see your key/properties are quoted: the fastest and easiest way to tackle your problem is my first suggestion: $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 Commented Apr 30, 2012 at 7:48

2 Answers 2

1

As Gubo pointed out: this is not a valid JSON encoded string. If the actual data you want to process in your script ís valid however, you're barking up the wrong tree looking for a regular expression... PHP has tons of functions that will parse JSON strings much faster than a regular expression.

$string1 = "{name:'something name here',type:'',id:''},{name:'othername',type:'small',id:34},{name:'orange',type:'weight',id:28}";
$string2 = '[{"name":"something name here","type":"","id":""},{"name":"othername","type":"small","id":"34"},{"name":"orange","type":"weight","id":"28"}]';

Where $string2 is the data in valid JSON formar. If your data is a valid JSON string, the following code will suffice:

$parsed = json_decode($string2);
//$parsed[0]['name'] return 'something name here'

If, however you're dealing with invalid JSON strings, things get a bit more complicated... First off: if you're lacking your object properties (or array keys as they will become in PHP) are quoted, a quick fix would be this:

$parsed = json_decode('['.$string1.']');

If you really want to parse them seperatly:

$separated= preg_split('/(?<=[\}]),/',$string1);

But I can't see why you would want to do that. The biggest issue here is the absence of quotes on the property strings (or keys). I have put together a regex (untested) that could quote those strings:

$parsed = json_decode(preg_replace('/(?<=[\{,])([a-z]+)/',str_replace('\'','"',$string1)));

Keep in mind, the last regex is untested, so it might not perform as you expect it to... but it should help you on your way... for the last example, the same rules apply for all the other examples I gave: if the quotes and brackets are there, just use json_decode, if the brackets are missing, add them, too...

It's getting rather late here, so I'm off to bed now... I hope this answer isn't packed with typo's and sentences that nobody can understand. If it is, I do apologize.

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed, the latest code example is not tested… “preg_repalce” ? :)
1

You don't need a regex for that. Just use this:

var_dump(json_decode($json, true));

See: http://us.php.net/manual/en/function.json-decode.php

1 Comment

His input was malformed, but the nature of the question was not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.