tags:

views:

81

answers:

4

Hello,

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.

A: 

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

antpaw
Won't work. `parse_str` (not parse-str) expects ampersands as key-value separators.
zneak
well thats why he needs to str_replace() it
antpaw
That wasn't there when I commented. (And it's going to fail if his user strings can contain ampersands.)
zneak
antpaw
That's getting into a lot of trouble for using a function that wasn't designed to do what you're making it do.
zneak
Shishant
But not a good way to improve your code.
zneak
A: 

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;
}
Tor Valamo
What's that manual iteration through a string? Strpos might be more cryptic, but it's a gazillion times faster.
zneak
One thing keep the habit of assigning values to a variable, because I will check strlen each time it loops
Shishant
I am trying to print_r and echo $arr but no values printed just Array ( )
Shishant
Did you try the second one? I know it works.
Tor Valamo
A: 

Have you considered parse_string, that would create an associative array based on the string you're listing above. This is how it would work:

$repd = str_replace( ' ', '&', 'accountid=10110 type=prem servertime=1263752255 validuntil=1266163393 username= '.
        'curfiles=11 curspace=188374868 bodkb=5000000 premkbleft=24875313' );
parse_str( $repd, $output );
var_dump( $output );

Outputs:

array(9) {
  ["accountid"]=>
  string(5) "10110"
  ["type"]=>
  string(4) "prem"
  ["servertime"]=>
  string(10) "1263752255"
  ["validuntil"]=>
  string(10) "1266163393"
  ["username"]=>
  string(0) ""
  ["curfiles"]=>
  string(2) "11"
  ["curspace"]=>
  string(9) "188374868"
  ["bodkb"]=>
  string(7) "5000000"
  ["premkbleft"]=>
  string(8) "24875313"
}
Christopher W. Allen-Poole
I am working with all examples with blank values and etc so that it doesn't fails when needed.
Shishant
Someone else answered it. :/ Why would you recommend using a function that was explicitly made for parsing GET/POST raw values to parse an unrelated string? That's not a good way to improve coding IMO. It's just adding limitations: you can't have ampersands anywhere in the string to be parsed, which is fine now, but if it ever is to change, your successor won't like it.
zneak
I know, I wasn't able to answer in time. More's the pity. As to what I would actually do -- my preference (and instinct) would be to actually use the preg_match functionality (though this is not what he was looking for), especially if he already knows what values to look for. If he knows that he wants servertime, he can do /servertime=([0-9]+)/ and only parse a small piece of the string instead of the whole thing. I often find that if I am asking this type of question, I would prefer a, "This is another possible solution" answer, although, in this case, the alternative is not superior.
Christopher W. Allen-Poole
+1  A: 

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.

zneak
Your function has error only 1st value of string is converted as key.
Shishant
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?
zneak