Thread: php help
View Single Post
  #2  
Old 07-11-2008, 08:00 PM
AndMetal
Developer
 
Join Date: Mar 2007
Location: Ohio
Posts: 648
Default

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:
Code:
  * 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):
Code:
[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 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:
PHP Code:
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:
PHP Code:
preg_match("/\*.*\*/"$ret[1], $matches);
$players[1][GM] = trim($matches[0], "* "); 
This will return:
Code:
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:
PHP Code:
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], 83);
    };
}; 
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.
__________________
GM-Impossible of 'A work in progress'
A non-legit PEQ DB server
How to create your own non-legit server

My Contributions to the Wiki
Reply With Quote