Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7859015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:43:45+00:00 2026-06-02T21:43:45+00:00

I try build a php regex that validate this type of input string: {name:’something

  • 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'; 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:44:05+00:00Added an answer on June 2, 2026 at 9:44 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i use this class for create HTML content http://code.google.com/p/php-class-html-generator/ I try to build a
i try to build a app that is in landsscape mode all the time.
Im try to build my plugins that sit in a seperate directory on the
When I try to build a project in Visual Studio 2005 that has a
I'm trying to build a quiz This is a sample of a try catch
I am going to try to build a PHP website using a framework for
I'm looking to build a PHP script that parses HTML for particular tags. I've
We've used php to build up an interface that the client can upload svg
So Im using Abrahams Twitter library like this: Connect.php /* Build TwitterOAuth object with
Whenever i build a URL i.e. cart.php?action=add&p[]=29&qty[]=3&p[]=5&qty[]=13 and try to grab the p variable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.