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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:30:16+00:00 2026-05-11T22:30:16+00:00

I have a function which accepts a string parameter such as: var1=val1 var2=val2 var3=’a

  • 0

I have a function which accepts a string parameter such as: “var1=val1 var2=val2 var3=’a list of vals'”;

I need to parse this string and pick out the var/val combination’s. That is easy enough until introducing something like var3=’a list of vals’. Obviously I can’t explode the string into an array using a white space delimiter which has me kind of stuck. I want to create an array from this string with the var/val pairs properly assigned, how can I do this in a case where I have something like var3?

  • 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-11T22:30:16+00:00Added an answer on May 11, 2026 at 10:30 pm

    if the format of the string is set in stone, you could do something like:

    $string = "var1=val1 var2=val2 var3='this is a test'";
    
    $vars = array();
    $i = 0;
    while ($i < strlen($string)) {
    
        $eqIndex = strpos($string, "=", $i);
        $varName = substr($string, $i, $eqIndex - $i);
    
        $i = $eqIndex + 1;
    
        if ($string[$i] == "'") 
        {
            $varEndIndex = strpos($string, "'", ++$i);
        }
        else
        {
            $varEndIndex = strpos($string, " ", $i);
            if ($varEndIndex === FALSE) $varEndIndex = strlen($string);
        }
    
        $varValue = substr($string, $i, $varEndIndex - $i);
    
        $vars[$varName] = $varValue;
    
        $i = $varEndIndex + 1;
    }
    
    print_r($vars);
    

    EDIT:

    More robust function that handles escaped chars in the quoted values:

    function getVarNameEnd($string, $offset) {
    
        $len = strlen($string);
        $i = $offset;
        while ($i < $len) {
    
            if ($string[$i] == "=")
                return $i;
            $i++;
        }
    
        return $len;
    }
    
    function getValueEnd($string, $offset) {
    
        $len = strlen($string);
        $i = $offset;
        if ($string[$i] == "'") {
            $quotedValue = true;
            $i++;
        }
        while ($i < $len) {
    
            if ($string[$i] == "\\" && $quotedValue)
                $i++;
            else if ($string[$i] == "'" && $quotedValue)
                return $i + 1;
            else if ($string[$i] == " " && !$quotedValue)
                return $i;
            $i++;
        }
    
        return $len;
    }
    
    function getVars($string) {
    
        $i = 0;
        $len = strlen($string);
        $vars = array();
        while ($i < $len) {
    
            $varEndIndex = getVarNameEnd($string, $i);
            $name = substr($string, $i, $varEndIndex - $i);
            $i = $varEndIndex + 1;
    
            $valEndIndex = getValueEnd($string, $i);
            $value = substr($string, $i, $valEndIndex - $i);
            $i = $valEndIndex + 1;
    
            $vars[$name] = $value;
        }
    
        return $vars;
    }
    
    $v = getVars("var1=var1 var2='this is a test' var3='this has an escaped \' in it' var4=lastval");
    print_r($v);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 155k
  • Answers 155k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I worked out a hack to use Twitter4j + Oauth… May 12, 2026 at 10:40 am
  • Editorial Team
    Editorial Team added an answer The point you are trying to centre the map onto… May 12, 2026 at 10:40 am
  • Editorial Team
    Editorial Team added an answer Yes, the straight forward way to do it is to… May 12, 2026 at 10:40 am

Related Questions

I have a function which accepts a string parameter such as: var1=val1 var2=val2 var3='a
I have an image I have manipulated with GD::Image and I want to do
Sometimes checking of arguments in Python is necessary. e.g. I have a function which
I have a common code of serializing a class object in my 3-4 methods
I'm working on an ASP.NET application that uses VB. I'm using a SQLReader/SQLCommand/SQLConnection within

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.