Problems with explode

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Problems with explode

Post 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?
Last edited by toasty2 on Sun Apr 08, 2007 3:22 pm, edited 4 times in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Post your code please.


Food for thought, what if the server name contains a question mark?
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Look carefully at which variable you are using in the output.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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.
Last edited by toasty2 on Sun Apr 08, 2007 3:27 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

What does this return?

Code: Select all

echo '<pre>';
print_r($srcds_d);
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I don't think you're really seeing a question mark as separator, but rather a character that cannot be represented...
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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?
Last edited by toasty2 on Sun Apr 08, 2007 3:31 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Change your browser charset and see what those characters actually are.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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?
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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;
?>
Post Reply