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 598811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:25:48+00:00 2026-05-13T16:25:48+00:00

Ok this is really difficult to explain in English, so I’ll just give an

  • 0

Ok this is really difficult to explain in English, so I’ll just give an example.

I am going to have strings in the following format:

key-value;key1-value;key2-...

and I need to extract the data to be an array

array('key'=>'value','key1'=>'value1', ... )

I was planning to use regexp to achieve (most of) this functionality, and wrote this regular expression:

/^(\w+)-([^-;]+)(?:;(\w+)-([^-;]+))*;?$/

to work with preg_match and this code:

for ($l = count($matches),$i = 1;$i<$l;$i+=2) {
    $parameters[$matches[$i]] = $matches[$i+1];
}

However the regexp obviously returns only 4 backreferences – first and last key-value pairs of the input string. Is there a way around this? I know I can use regex just to test the correctness of the string and use PHP’s explode in loops with perfect results, but I’m really curious whether it’s possible with regular expressions.

In short, I need to capture an arbitrary number of these key-value; pairs in a string by means of regular expressions.

  • 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-05-13T16:25:48+00:00Added an answer on May 13, 2026 at 4:25 pm

    You can use a lookahead to validate the input while you extract the matches:

    /\G(?=(?:\w++-[^;-]++;?)++$)(\w++)-([^;-]++);?/
    

    (?=(?:\w++-[^;-]++;?)++$) is the validation part. If the input is invalid, matching will fail immediately, but the lookahead still gets evaluated every time the regex is applied. In order to keep it (along with the rest of the regex) in sync with the key-value pairs, I used \G to anchor each match to the spot where the previous match ended.

    This way, if the lookahead succeeds the first time, it’s guaranteed to succeed every subsequent time. Obviously it’s not as efficient as it could be, but that probably won’t be a problem–only your testing can tell for sure.

    If the lookahead fails, preg_match_all() will return zero (false). If it succeeds, the matches will be returned in an array of arrays: one for the full key-value pairs, one for the keys, one for the values.

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

Sidebar

Related Questions

Ok, this is really difficult to confess, but I do have a strong temptation
This is going to be difficult to explain but hopefully there will be a
This might be a bit difficult to explain but I have two columns in
This is a bit difficult to explain, so I'll try to give a simple
This is difficult to explain, please bear with me. I have a top menu
It's difficult to explain what I really want. I have an interface which has
I have a really difficult problem to solve and Im just wondering what what
I'm having a really difficult time debugging this problem. Whenever I try to establish
This really shouldn't be hard, I just can't figure out how to do it.
hey guys having this really simple problem but cant seem to figure out have

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.