Jump to content

Working with arrays


FVxSF

Recommended Posts

Not like, "how do you make one" (well kinda), or "how do you get last item".. Here's my problem, I working on a nice little script to read server stats from a battlefield 2142 server. So far what I have is working really really good, but then I get a little quirk. For and example What I'm doing is creating an array by exploding. All the information about the server players is in one long string so what I have done is convert the identifiers to a unique group then explode based on the group say _::_ for an example.

 

This works great because now I have all the player, pings, kills and so on in one array but every once in a while I'll get the identifier 2 time. Like below say:

 

ping_ 54 75 25 75 kills_ 5 4 7 8 7 kills_ 6 7 5 1

 

Now I'm using the str_replace to replace the identifiers (in this case ping_ and kills_) but when I go to explode the players string the data get screwed up because kills_ is there 2 times.

 

So my question is, how can I combine the 2 kills_ in to the same array?

Link to comment
Share on other sites

I see your problem, but why does it sometimes return two kills? Which would you need to actually use in that case aswell?

 

Adam

 

If "kills_" or what ever is stored as an array key, then PHP should display an error if it exists twice (could be wrong). Regardless though, from my experience, if it's a key then the first array element will be overwritten by the second.

Link to comment
Share on other sites

This is a little confusing I know. I'll show you what I'm working with:

 

<?php

$sock = fsockopen("udp://". $_GET[ip], 29900);
socket_set_timeout($sock, 0, 750000);

$end = false;
@fwrite($sock, "\xFE\xFD\x09\x01\x01\x01\00");
while( !$end ) {
	$bytes = @fread($sock, 1);
	$sock_status = @socket_get_status($sock);
	$length = $sock_status[unread_bytes];

	if( $length ) {
		$reply = $bytes . fread($sock, $length);
		$challenge = explode("\x00", $reply);
		$challenge_reply = str_split(dechex($challenge[1]), 2);
		$sock_challenge = chr(hexdec($challenge_reply[0])). chr(hexdec($challenge_reply[1])). chr(hexdec($challenge_reply[2])). chr(hexdec($challenge_reply[3]));
	}
	if( $length == 0 ) { $end = true; }
}
@fwrite($sock, "\xFE\xFD\x00\x01\x01\x01\00". $sock_challenge ."\xFF\xFF\xFF\x01");
$end = false; $data = "";

while( !$end ) {
	$bytes = @fread($sock, 1);
	$sock_status = @socket_get_status($sock);
	$length = $sock_status[unread_bytes];

	if( $length ) {
		$data .= $bytes . fread($sock,$length);
	} else { 
		$end = true;
	}
}
fclose($sock);

if( !$data ) {
	echo "Couldn't get info from server";
	exit;
}

$data=str_replace("\\","",$data);				
$data=str_replace(chr(0).chr(1).chr(1).chr(1).chr(0)."splitnum".chr(0).chr(0).chr(0),"",$data);
$data=str_replace(chr(0).chr(1).chr(1).chr(1).chr(0)."splitnum".chr(0).chr(128).chr(0),"",$data);
$data=str_replace(chr(0).chr(1).chr(1).chr(1).chr(0)."splitnum".chr(0).chr(129).chr(1),"",$data);
$data=str_replace(chr(0).chr(1).chr(1).chr(1).chr(0)."splitnum".chr(0).chr(129).chr(2),"",$data);
$data=str_replace(chr(0).chr(1).chr(1).chr(1).chr(0)."splitnum".chr(0).chr(130).chr(1),"",$data);
$data=str_replace(chr(0).chr(1).chr(1).chr(1).chr(0)."splitnum".chr(0).chr(130).chr(2),"",$data);
$data=str_replace(chr(0).chr(1).chr(1).chr(1).chr(0)."splitnum".chr(0).chr(1).chr(1),"",$data);
$data=str_replace(chr(0).chr(0).chr(0),chr(0),$data);

$PInf = substr($data, strpos($data, "player_"), strpos($data, "team_t") - strpos($data, "player_"));

$RplArray = array( // Replacement Array \\
			"player_". chr(0).chr(0) => "",
			"score_" => "::",
			"ping_" => "::",
			"team_" => "::",
			"deaths_" => "::",
			"pid_" => "::",
			"skill_" => "::"
);

$PInfT = str_replace(array_keys($RplArray), array_values($RplArray), $PInf);
$PlayerInArr = explode(chr(0).chr(0) ."::". chr(0).chr(0), $PInfT);


echo $PInf;
echo "\n\n<br><br><hr><br><br>\n\n";

echo "<pre>";
echo var_dump($PlayerInArr) ;
echo "</pre>";

?>

 

First, I don't know much about fsock but I know about bytes and so forth.

After receiving the information I'm after I get the player info like this:

 

player_� Vladiemier�=FCW= LordVaderOfFCW�=fcw= ViperTheSniper�score_��0�0�0��ping_��106�50�54��team_��2�2�1��deaths_��0�0�0��pid_��120276091�65616121�84831881��skill_��0�0�0�

 

Now above there are byte values of 0 "chr(0)" as the serperator, there are 2 bytes after ping_ and player_ and so on, both have a value of 0, chr(0) if you will.

 

Now this is some example of what I need to fix:

 

pid_ chr(0) chr(0) 13294453 "chr(0)" 1�146236060 "chr(0)" �154316543� chr(0) 93162469�10 chr(0) 2633384� chr(0) 157920896 chr(0) pid_136585560� chr(0) 91831085��

 

I added chr(0) as the seperator, but this will happen from time to time. You can try this link -> http://fvxsf.com/archive/fetcher.php?ip=69.12.25.110

you many need to refresh until you see data. Also I changed player_ score_ and so on to :: so if you see :: that mean something like pid_ came up 2 times.

Link to comment
Share on other sites

As a follow up, I did manage to fix this problem. What I saw was that there were 2 bytes padding the left and right of the player_ score_ and so on, so I what I did was explode my array based on that because if I did get a stray score_ the left padding was missing 1 byte. So to fix that what I was create an array and used str_replace to trim out what I don't what.

 

Now that I have this fixed, I'm noticing something every now and then. Some times a letter of a name will turn out to be some random byte but if that page is refreshed it goes away.

 

Is there a way to take the byte strings and convert them from raw upd data to something like ascii or utf8?

 

Here is an example:

http://fvxsf.com/archive/fetcher.php?ip=208.122.52.162

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.