PDA

View Full Version : php help


spider661
07-10-2008, 02:32 PM
im trying to put in my website a who all online thingy.

i can list all the players online with a telnet connection but it outputs more info then i need

so i want to strip our everything i don't need and only keep what i do and i have always had problems with striping so i figured i would ask for help.

i am using this.


fConnect($fp,$Server,$Port,$user,$pass);

fputs ($fp, "who\r");
usleep(125000);


while (!feof($fp))
{
$ret = fgets($fp);
if (ereg("zone", $ret)) {
echo "$ret \n <br>";
}
if (ereg("online", $ret)){
echo "$ret \n";
fclose($fp);
break;
}
}


and it outputs something like this


[59 Master] Yuari (Iksar) zone: sebilis AccID: 298 AccName: Flora LSID: 110561 Status: 0

1 player online


i want to strip out everything and make a table that says

level name race class zone
and then strips everything and places relevant info under the right catagory..

can someone help me with this?

AndMetal
07-11-2008, 08:00 PM
In general, you're probably going to want to use Regular Expressions, although it's gonna be a little complicated.

When I log into my GM character, this is everything that's returned with the "who" command in the telnet window:
* GM-Impossible * [ANON 70 Stone Fist] Xamlagar (Iksar) zone: poknowledge AccID: 13 AccName: AndMetal LSID: 87737 Status: 250
And this is what you get with a regular character (not sure about any leading spaces):
[59 Master] Yuari (Iksar) zone: sebilis AccID: 298 AccName: Flora LSID: 110561 Status: 0

The first issue is that the string doesn't have the same delimiters every time. You could use explode (http://us3.php.net/manual/en/function.explode.php) with a space as the delimiter to start separating it, but because several items can be multiple words (class & race off the top of my head), that doesn't really help. So, back to regular expressions.

First, we'll start by pulling just the character info:

fConnect($fp,$Server,$Port,$user,$pass);

fputs ($fp, "who\r");
usleep(125000);

while (!feof($fp)) {
$ret[++$x] = fgets($fp);
if (ereg("zone", $ret[$x])) {
fclose($fp);
break;
};
};

So now we can work with $ret.

To separate any GM flag, you should be able to use this:

preg_match("/\*.*\*/", $ret[1], $matches);
$players[1][GM] = trim($matches[0], "* ");

This will return:
GM-Impossible
for the GM character & a blank for a regular character. You could then use the same idea for the rest. This is the script I came up with:

foreach ($ret as $key => $value) {
// GM Status
if (preg_match("/\*.*\*/", $value, $matches)) {
$players[$key][GM] = trim($matches[0], "* ");
};
// Role/Anon, Level, & Class
if (preg_match("/\[.*\]/", $value, $matches)) {
$tmp = explode(" ", trim($matches[0], "[] "));
if (!is_numeric($tmp[0])) {
$players[$key][Visible] = $tmp[0];
$players[$key][Level] = $tmp[1];
$players[$key]["Class"] = rtrim($tmp[2] . " " . $tmp[3], " ");
} else {
$players[$key][Level] = $tmp[0];
$players[$key]["Class"] = rtrim($tmp[1] . " " . $tmp[2], " ");
};
};
// Character's Name
if (preg_match("/\].*\(/", $value, $matches)) {
$players[$key][Name] = trim($matches[0], "] (");
};
// Race
if (preg_match("/\(.*\)/", $value, $matches)) {
$players[$key][Race] = trim($matches[0], "( )");
};
// Zone
if (preg_match("/zone:.*AccID:/", $value, $matches)) {
$players[$key][ZoneLong] = substr($matches[0], 6, -7);
};
// Account ID
if (preg_match("/AccID:.*AccName:/", $value, $matches)) {
$players[$key][AcctID] = substr($matches[0], 7, -9);
};
// Account Name
if (preg_match("/AccName:.*LSID:/", $value, $matches)) {
$players[$key][AcctName] = substr($matches[0], 9, -6);
};
// Login Server ID
if (preg_match("/LSID:.*Status:/", $value, $matches)) {
$players[$key][LSID] = substr($matches[0], 6, -8);
};
// Status
if (preg_match("/Status:.*/", $value, $matches)) {
$players[$key][Status] = substr($matches[0], 8, 3);
};
};


Now you have a nice, simple array of all the characters that you can foreach to output to a table. The only thing missing would be anything that has to do with Guilds, but that should be easy enough to add.

I'm sure there's more efficient ways of doing this, especially if you can pull just what you're looking for the with Regular Expression instead of everything around it, but this will at least get the job done.

Hope this helps.

spider661
07-12-2008, 04:05 PM
ok i tried doing a print_r($players); and got an error.

Warning: Invalid argument supplied for foreach() in E:\wamp\www\who.php on line 46

for each player online

btw this is after adding to my code so line 46 is the first line there.
foreach ($ret as $key => $value) {


what should i do and am i calling the print_r($players); correctly or should i do something else. im trying to print it so i can see all the values of the array so i can place them in the right tables

AndMetal
07-14-2008, 12:16 AM
For some reason, my Telnet code made sense the other day, but after looking at it now, I'm not sure where me head was :confused:

Here is the entire script I put together for testing:

<?php

$ret[1] = " * GM-Impossible * [ANON 70 Stone Fist] Xamlagar (Iksar) zone: poknowledge AccID: 13 AccName: AndMetal LSID: 87737 Status: 250";
$ret[2] = " [59 Master] Yuari (Iksar) zone: sebilis AccID: 298 AccName: Flora LSID: 110561 Status: 0";
$ret[3] = " [70 Performer] Keasi (Half Elf) zone: poknowledge AccID: 1777 AccName: Keasi LSID: 69583 Status: 0";
$ret[4] = " [70 Vanquisher] Suddz (Iksar) zone: cabeast AccID: 1780 AccName: OcelotRX LSID: 110949 Status: 0";

echo "<pre>\n\$ret = ";
print_r($ret);
echo "\n</pre>\n";

foreach ($ret as $key => $value) {
// GM Status
if (preg_match("/\*.*\*/", $value, $matches)) {
$players[$key][GM] = trim($matches[0], "* ");
};
// Role/Anon, Level, & Class
if (preg_match("/\[.*\]/", $value, $matches)) {
$tmp = explode(" ", trim($matches[0], "[] "));
if (!is_numeric($tmp[0])) {
$players[$key][Visible] = $tmp[0];
$players[$key][Level] = $tmp[1];
$players[$key]["Class"] = rtrim($tmp[2] . " " . $tmp[3], " ");
} else {
$players[$key][Level] = $tmp[0];
$players[$key]["Class"] = rtrim($tmp[1] . " " . $tmp[2], " ");
};
};
// Character's Name
if (preg_match("/\].*\(/", $value, $matches)) {
$players[$key][Name] = trim($matches[0], "] (");
};
// Race
if (preg_match("/\(.*\)/", $value, $matches)) {
$players[$key][Race] = trim($matches[0], "( )");
};
// Zone
if (preg_match("/zone:.*AccID:/", $value, $matches)) {
$players[$key][ZoneLong] = substr($matches[0], 6, -7);
};
// Account ID
if (preg_match("/AccID:.*AccName:/", $value, $matches)) {
$players[$key][AcctID] = substr($matches[0], 7, -9);
};
// Account Name
if (preg_match("/AccName:.*LSID:/", $value, $matches)) {
$players[$key][AcctName] = substr($matches[0], 9, -6);
};
// Login Server ID
if (preg_match("/LSID:.*Status:/", $value, $matches)) {
$players[$key][LSID] = substr($matches[0], 6, -8);
};
// Status
if (preg_match("/Status:.*/", $value, $matches)) {
$players[$key][Status] = substr($matches[0], 8, 3);
};
};

echo "<pre>\n\$players = ";
print_r($players);
echo "\n</pre>\n";

?>

And it works fine. However, it doesn't look like the Telnet code I suggested is putting out the array $ret, which is why it's freaking out at the foreach. Try this instead:

fConnect($fp,$Server,$Port,$user,$pass);

fputs ($fp, "who\r");
usleep(125000);

while (!feof($fp)) {
$ret_tmp = fgets($fp);
if (ereg("zone", $ret_tmp)) {$ret[++$x] = $ret_tmp;};
if (ereg("online", $ret_tmp)) {break;};
};
fclose($fp);

That should put just the players into $ret & skip everything else, instead of just stopping the while loop & not outputting anything useful.

spider661
07-14-2008, 07:56 AM
ok this is working but i cant figeur out how to call the arrays to put them wher ei want them in a table

i tried echo $players["Class"]; and echo $players[1]; and echo $players["Class"][1];

nothing is working.

spider661
07-14-2008, 08:55 AM
nvm i got it working $players[1]['Class']; where q is the player and 'class' is what im looking for..

i do see a problem though it messes up if there zoneing. i would just check if there zoneing and put zoneing in all the fields but the name messes up also so i cant say there name.. is there a way to put in a zoneing check of the name at lest?

spider661
07-14-2008, 09:15 AM
this is what i see when someone is zoneing
$ret = Array
(
[1] => [33 Shadowknight] Darkelf (Dark Elf) zone: qrg AccID: 250 AccName: noidster5 LSID: 112420 Status: 0

[2] => [56 Conjurer] Divad (Erudite) zone: veksar AccID: 141 AccName: lordraydin LSID: 109711 Status: 0

[3] => [0 Unknown] Everyone (Unknown) zone: (null) AccID: 319 AccName: dhgoyer LSID: 112843 Status: 0

)


Level Class Name Race Zone

33 Shadowknight Darkelf Dark Elf qrg
56 Conjurer Divad Erudite veksar
0 Unknown Everyone (Unknown) zone: Unknown) zone: (null (null)
3 players online