0

I am using an api to retrieve data from another server the data returned is something like this:

accountid=10110 type=prem servertime=1263752255 validuntil=1266163393 
username= curfiles=11 curspace=188374868 bodkb=5000000 premkbleft=24875313

This is a whole string I need two values out of whole string, I am currently using preg_match to get it, but just to learn more and improve my coding is there any other way or function in which all values are automatically convert to array?

Thank You.

3 Answers 3

1

Sooo, my faster-than-preg_split, strpos-based function looks like this:

function unpack_server_data($serverData)
{
    $output = array();
    $keyStart = 0;
    $keepParsing = true;

    do
    {
        $keyEnd = strpos($serverData, '=', $keyStart);
        $valueStart = $keyEnd + 1;
        $valueEnd = strpos($serverData, ' ', $valueStart);
        if($valueEnd === false)
        {
            $valueEnd = strlen($serverData);
            $keepParsing = false;
        }

        $key = substr($serverData, $keyStart, $keyEnd - $keyStart);
        $value = substr($serverData, $valueStart, $valueEnd - $valueStart);
        $output[$key] = $value;

        $keyStart = $valueEnd + 1;
    }
    while($keepParsing);

    return $output;
}

It looks for an equals character, then looks for a space character, and uses these two to decide where a key name begins, and when a value name begins.

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

2 Comments

Your function has error only 1st value of string is converted as key.
I tried with the string you provided us and it yields good results. Did you test with this string too? If you didn't, can you post the one you used?
0

Using explode is the fastest for this, no matter what.

However, to answer you question, you can do this many ways. But just because you can, doesn't mean you should. But if you really wanna make it weird, try this.

UPdated using strpos

$arr = array();
$begin = 0;
$str = trim($str); # remove leading and trailing whitespace
while ($end = strpos($str, ' ', $begin)) {
    $split = strpos($str, '=', $begin);
    if ($split > $end) break;
    $arr[substr($str, $begin, $split-$begin)] = substr($str, $split+1, $end-$split-1);
    $begin = $end+1;
}

3 Comments

What's that manual iteration through a string? Strpos might be more cryptic, but it's a gazillion times faster.
One thing keep the habit of assigning values to a variable, because I will check strlen each time it loops
I am trying to print_r and echo $arr but no values printed just Array ( )
0

try out parse_str maybe you need to do str_replace(' ', '&', $string); before

5 Comments

Won't work. parse_str (not parse-str) expects ampersands as key-value separators.
well thats why he needs to str_replace() it
That wasn't there when I commented. (And it's going to fail if his user strings can contain ampersands.)
yes thats true, maybe it would help if he first "masks" the text ampersands with str_replace('&', 'crazychar', $string) and after parse_str replace it back.
That's getting into a lot of trouble for using a function that wasn't designed to do what you're making it do.

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.