PDA

View Full Version : Help with commas in RealChat rooms string


Boofo
07-09-2004, 12:32 AM
Could anyone please help me add commas to the string of RealChat rooms that the following code generates?

// REALCHAT WHO'S ONLINE
$port = 10010;
function getServerAPI( $apiCommand ) {
global $port;
$result = "";
$fp = fsockopen("localhost", $port, &$errno, &$errstr, 2);
if(!$fp) {
echo "$errstr ($errno)\n";
} else {
fputs($fp,"GET /?".$apiCommand." HTTP/1.0\n\n");
$header = true;
while(!feof($fp)) {
$line = fgets($fp,128);
if ( $header == false ) $result .= $line;
if ( trim($line) == "" ) $header = false;
}
fclose($fp);
}
return $result;
}
$userCount = getServerAPI( "api.UserCount" );
$roomList = getServerAPI( "api.RoomList" );
// End REALCHAT WHO'S ONLINE

Right now it looks like this:

Available RealChat Rooms: The Bear's Den The Lobby Fun Staff only! No topic Bear's DenThe rooms are:

The Bear's Den
The Lobby
Fun
Staff only!
No topic
Bear's Den

I would like commas after the names except for the last one.

This is the code that came with it so I have no idea how to add the commas.

Thanks in advance. ;)

Velocd
07-09-2004, 04:16 AM
Assuming that each channel is being retrieved on its own line, you should be able to:


function getServerAPI($apiCommand)
{
global $port;

$result = "";

$fp = fsockopen("localhost", $port, &$errno, &$errstr, 2);

if(!$fp)
{
echo "$errstr ($errno)\n";
}
else
{
fputs($fp,"GET /?".$apiCommand." HTTP/1.0\n\n");

$header = true;

while(!feof($fp))
{
$line = fgets($fp,128);

// print $line;

if ($header == false)
$result .= $line . ', ';

if (trim($line) == "")
$header = false;
}

fclose($fp);
}

return substr($result, 0, -2); // trim the last ', '
}


To get the comma separated list. If it's not separated by a line, uncomment the echo I have in the loop and check the output.

Boofo
07-09-2004, 04:57 AM
Assuming that each channel is being retrieved on its own line, you should be able to:


function getServerAPI($apiCommand)
{
global $port;

$result = "";

$fp = fsockopen("localhost", $port, &$errno, &$errstr, 2);

if(!$fp)
{
echo "$errstr ($errno)\n";
}
else
{
fputs($fp,"GET /?".$apiCommand." HTTP/1.0\n\n");

$header = true;

while(!feof($fp))
{
$line = fgets($fp,128);

// print $line;

if ($header == false)
$result .= $line . ', ';

if (trim($line) == "")
$header = false;
}

fclose($fp);
}

return substr($result, 0, -2); // trim the last ', '
}


To get the comma separated list. If it's not separated by a line, uncomment the echo I have in the loop and check the output.Ok that almost works. We have an extra space between the room name and the commas, though. Like this:

Available RealChat Rooms: The Bear's Den , The Lobby , Fun , Staff only! , No topicHow do I take the extra space out of that?

And thank you, very much, sir. ;)

Velocd
07-09-2004, 07:00 PM
Replace:
$result .= $line . ', ';

With:
$result .= substr($line, 0, -1) . ', ';

Should do it.

Boofo
07-09-2004, 07:45 PM
Excellent! Worked like a charm! Thank you, sir. ;)

Modin
07-09-2004, 10:03 PM
you could also use the php trim() function. It removes all surrounding whitespace characters, which is very helpful when dealing with strings.

Boofo
07-09-2004, 10:18 PM
you could also use the php trim() function. It removes all surrounding whitespace characters, which is very helpful when dealing with strings.
How would you incorporate that into this?

Velocd
07-10-2004, 04:15 AM
Instead of doing:

$result .= substr($line, 0, -1) . ', ';

Use:

$result .= trim($line) . ', ';

Probably isn't any faster, but it's more appropriate.

Boofo
07-10-2004, 05:50 AM
Instead of doing:

$result .= substr($line, 0, -1) . ', ';

Use:

$result .= trim($line) . ', ';

Probably isn't any faster, but it's more appropriate.
Thank you, again, sir. ;)

For future reference, will the trim get rid of whitespace on both sides of the line or just the preceding whitespace?

Boofo
07-10-2004, 01:21 PM
I am getting a comma after the amount of users in the chat room randonly now. It doesn't happen all the time but every once in a while. Like this:

RealChat Users Connected: 0,
Is there any way to prevent this?

Velocd
07-10-2004, 03:40 PM
Is that output from the function above? If not, post that code, and I'll take a look at it.

Do refer to the substr function above for truncating strings. A how-to-use reference for it is here:
http://www.php.net/substr

Also, trim:
http://www.php.net/trim

Boofo
07-10-2004, 04:07 PM
Is that output from the function above? If not, post that code, and I'll take a look at it.

Do refer to the substr function above for truncating strings. A how-to-use reference for it is here:
http://www.php.net/substr

Also, trim:
http://www.php.net/trimOk, thanks! ;)

That is from this:

$userCount = getServerAPI( "api.UserCount" );


in my first post at the bottom of the code. It's all part of the same code. The wierd thing is it doesn't do it all of the time. Only about every 5th time or so. Sometimes more, sometimes less.

esology
06-02-2005, 11:17 PM
I used these tips as a guideline for my RealChat implementation. I sure appreciate this post. I have a question though, whenever my rooms are empty this is what is diplayed.

ERROR: Room empty.
ERROR: Room empty.

Any ideas.