Forum Moderators: coopster

Message Too Old, No Replies

Parse string

parse string

         

ScorpSoul

4:17 pm on Jun 17, 2008 (gmt 0)

10+ Year Member



Hey, this kinda sounds lazy but I really can't understand the parsers...
I need to parse stuff like this:

"rm_map_statistics"
{
"de_dust"
{
"total_votes" "100"
"total_excellent_votes" "50"
"total_good_votes" "20"
"total_average_votes" "20"
"total_poor_votes" "8"
"total_very_poor_votes" "2"
"STEAM_ID_LAN" "very poor"
}
"zm_lolol"
{
"total_votes" "100"
"total_excellent_votes" "50"
"total_good_votes" "20"
"total_average_votes" "20"
"total_poor_votes" "8"
"total_very_poor_votes" "2"
"STEAM_ID_LAN" "very poor"
}
"cs_lololol"
{
"total_votes" "100"
"total_excellent_votes" "50"
"total_good_votes" "20"
"total_average_votes" "20"
"total_poor_votes" "8"
"total_very_poor_votes" "2"
"STEAM_ID_LAN" "very poor"
}
}

Is there anyway that I can put all this values for each map on an array?

I've tried a parse that my friend made for me

$regs = preg_match('/"([^"])"[^\{]*\{([^\}]*)}/',$get);

but the outcome was just "1"

cameraman

4:51 pm on Jun 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, ScorpSoul.

Your friend's regex is pretty close. The '1' you're seeing is the number of matches it found, which would be either 0 or 1 for preg_match. Try this:
$matches = preg_match_all('#"(.*)" "(.*)"#m',$get,$answers);
print_r($answers);

The resulting array would be more meaningful if you broke up the maps first.

ScorpSoul

10:23 pm on Jun 19, 2008 (gmt 0)

10+ Year Member



Tks for the reply.
However the result was:
Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) )

And I just realised the preg_match that he gave me.. to parse this should't it be preg_split?

cameraman

6:48 am on Jun 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And $get contains the block of stuff you posted above? That's odd, my experiments worked ok.
preg_split is fine, but I don't think your friend's pattern is quite right for what you posted. I'm not a regex guru though, more of a hack, so I could be wrong.