Page 1 of 1

Problems with explode

Posted: Sun Apr 08, 2007 3:07 pm
by toasty2
I am trying to query a source engine server and format the information it sends back into a nice, readable form.

By a source engine dedicated server, I mean a server such as a Counter-Strike: Source server (a game server).

I found some code on the internet that gets info from servers and here's the output for when I query a LAN server which I set up:

Code: Select all

Testing Server�cs_office�cstrike�Counter-Strike: Source�ð�� �dw1.0.0.34�
I noticed that it separates things with a question mark, so I figured I could use explode to separate all the info. I have it spit the output above into a string, so here's my code:

Code: Select all

$srcds_d = explode("?", $srcds_data);
echo "Server Name: $srcds_d[0] <br>";
echo "Map: $srcds_d[1] <br>";
It outputs nothing after "Server Name:" and "Map:", and I have confirmed that $srcds_data does have the returned info in it. What's wrong?

Posted: Sun Apr 08, 2007 3:10 pm
by John Cartwright
Post your code please.


Food for thought, what if the server name contains a question mark?

Posted: Sun Apr 08, 2007 3:16 pm
by toasty2
Sorry, I wasn't completely finished when you posted. Its there now.

As for the question mark thing, I'm not sure...but my server doesn't have a question mark in the name, so I'm not worried about that yet.

Posted: Sun Apr 08, 2007 3:21 pm
by John Cartwright
Look carefully at which variable you are using in the output.

Posted: Sun Apr 08, 2007 3:23 pm
by toasty2
In my actual code, I am using the right variable. I updated the first post. I had the old code on my clipboard and pasted it when i made this topic instead of copying and pasting the most recent code. It doesn't work even with the correct code. Sorry for the error.

Posted: Sun Apr 08, 2007 3:27 pm
by John Cartwright
What does this return?

Code: Select all

echo '<pre>';
print_r($srcds_d);

Posted: Sun Apr 08, 2007 3:27 pm
by timvw
I don't think you're really seeing a question mark as separator, but rather a character that cannot be represented...

Posted: Sun Apr 08, 2007 3:30 pm
by toasty2
It's really long, so I won't put it in this post. Instead, i saved it into a text file: http://server.randomresources.com/print_r.txt
timvw wrote:I don't think you're really seeing a question mark as separator, but rather a character that cannot be represented...
Is there a way I can verify this?

Posted: Sun Apr 08, 2007 3:30 pm
by RobertGonzalez
Change your browser charset and see what those characters actually are.

Posted: Sun Apr 08, 2007 3:34 pm
by toasty2
What charset would you recommend? I tried UTF-8 (I'm using Firefox) and I get the same thing:

Code: Select all

Testing Server&#65533;cs_office&#65533;cstrike&#65533;Counter-Strike: Source&#65533;ð&#65533;&#65533; &#65533;dw1.0.0.34&#65533;
In IE7 (default charset), it gives me:

Code: Select all

Testing Servercs_officecstrikeCounter-Strike: Sourceð dw1.0.0.34
How do I figure out what character I'm dealing with here?

Edit: In IE7 with UFT-8, I see a square or two.


Here is the specification for querying the server: http://developer.valvesoftware.com/wiki ... t_format_2
Also, this page might help: http://dev.kquery.com/index.php?article=46

How can i separate the data being returned?

Posted: Sun Apr 08, 2007 5:21 pm
by toasty2
Now I have another problem, aside from not being able to separate the info.

It displays $srcds_raw from inside the while, but it doesn't display the one outside of the while.

Here's the code:

Code: Select all

<?php
if (substr_count($_GET['address'], ":") >= "1")
{
$cut = explode(":", $_GET['address']);
$HL2_address = $cut[0];
$HL2_port = $cut[1];
}

else
{
// Default port 27015
$HL2_address = $_GET['address'];
$HL2_port = "27015";
}

$HL2_command = "\377\377\377\377TSource Engine Query\0";
$HL2_socket = fsockopen("udp://".$HL2_address, $HL2_port, $errno, $errstr);
fwrite($HL2_socket, $HL2_command); $JunkHead = fread($HL2_socket,4);
$CheckStatus = socket_get_status($HL2_socket);

if($CheckStatus["unread_bytes"] == 0)
{
$srcds_status = "Offline";
die();
}

$do = 1;
while($do)
{
$str = fread($HL2_socket,1);
$HL2_stats.= $str;
$status = socket_get_status($HL2_socket);

if($status["unread_bytes"] == 0)
{
$do = 0;
}
}
fclose($HL2_socket);
$x = 0;

while ($x <= strlen($HL2_stats))
{
$x++;
global $srcds_raw;
$srcds_raw = substr($HL2_stats, $x, 1);
global $srcds_raw;
echo $srcds_raw;
}
echo "<br>";
echo $srcds_raw;
?>