spider661 Posted July 10, 2008 Share Posted July 10, 2008 i always have a problem manipulating string and i need to do some for a website im working on. so i was wondering if anyone could help me out? i want to take a string and strip parts of it out and then post them parts.. would be helpful if it could make them a new variable usable on other pages also but thats not important i really just need to strip the info out. i have this code to get the string 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 returns a string that looks like this [72 Stone Fist] Yuari (Iksar) zone: gfaydark AccID: 298 AccName: Flora LSID: 110561 Status: 0 [72 Soothsayer] Kefus (Iksar) zone: qrg AccID: 307 AccName: Nerine LSID: 110954 Status: 0 [74 Wildblood] Tigger (Vah Shir) zone: gukbottom AccID: 244 AccName: harkenus2 LSID: 112355 Status: 0 3 players online first part the numbers i need thats there level and needs to go in a table under the title level next part (on first line its) stone fist, i need that its there class and needs to go under class next part is there name yuari and that needs to go under name. next part iskar is there class and needs to go under class. and the final part is gfaydark and its there zone and needs to go under zone. and thats everything i need so when looking at the output on a website i would like it to make a table and read like so Name level class race zone Yuari 72 stone fist iskar gfaydark then the next person and so on till no more. can anyone help me with this? Quote Link to comment https://forums.phpfreaks.com/topic/114169-solved-manipulating-string/ Share on other sites More sharing options...
spider661 Posted July 11, 2008 Author Share Posted July 11, 2008 ok i got something working here useing this code function substring_between($haystack,$start,$end) { if (strpos($haystack,$start) === false || strpos($haystack,$end) === false) { return false; } else { $start_position = strpos($haystack,$start)+strlen($start); $end_position = strpos($haystack,$end); return substr($haystack,$start_position,$end_position-$start_position); } } i did this fConnect($fp,$Server,$Port,$user,$pass); fputs ($fp, "who\r"); usleep(125000); echo '<center><table border="4" cellpadding="4" cellspacing="4" ALIGN="CENTER"> <tr> <th>Name</th> <th>Race</th> <th>Level/Class</th> <th>Zone</th> </tr>'; while (!feof($fp)) { $ret = fgets($fp); if (ereg("zone", $ret)) { $name = substring_between($ret, '] ', ' ('); $race = substring_between($ret, '(', ')'); $level = substring_between($ret, '[', ']'); $zone = substring_between($ret, 'zone: ', ' AccID:'); echo "<tr> <td class='smalltable'> $name </td> <td class='smalltable'> $race </td> <td class='smalltable'> $level </td> <td class='smalltable'> $zone </td> </tr>"; //echo "$ret1 \n <br>"; } if (ereg("online", $ret)){ echo "$ret \n"; fclose($fp); break; } } echo '</table><center>'; and it outputs a table that looks like this Name Race Level/Class Zone Irun High Elf 75 Arch Magus delveb Southern Wood Elf ANNON 75 Natureguard butcher 2 players online now thats great but i would like to split up Level/Class and take out annon if its there and then put lvl and class in a diff row. the problem is the function above will not take a space as $start or $end.. anyone got any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/114169-solved-manipulating-string/#findComment-587317 Share on other sites More sharing options...
sasa Posted July 11, 2008 Share Posted July 11, 2008 try <?php $ret = '[72 Stone Fist] Yuari (Iksar) zone: gfaydark AccID: 298 AccName: Flora LSID: 110561 Status: 0 [72 Soothsayer] Kefus (Iksar) zone: qrg AccID: 307 AccName: Nerine LSID: 110954 Status: 0 [74 Wildblood] Tigger (Vah Shir) zone: gukbottom AccID: 244 AccName: harkenus2 LSID: 112355 Status: 0 3 players online'; preg_match_all('/\[(?P<level>\d+)(?P<class>[^\]]*)\](?P<name>[^\(]+)\((?P<race>[^\)]+)\)\s*zone:\s*(?P<zone>[\S]+)\s/i', $ret, $out); print_r($out); ?> Quote Link to comment https://forums.phpfreaks.com/topic/114169-solved-manipulating-string/#findComment-587339 Share on other sites More sharing options...
spider661 Posted July 11, 2008 Author Share Posted July 11, 2008 dont think it worked but looks close to what i want but i think there all blank this is what it printed out Array ( [0] => Array ( ) [level] => Array ( ) [1] => Array ( ) [class] => Array ( ) [2] => Array ( ) [name] => Array ( ) [3] => Array ( ) [race] => Array ( ) [4] => Array ( ) [zone] => Array ( ) [5] => Array ( ) ) Array ( [0] => Array ( ) [level] => Array ( ) [1] => Array ( ) [class] => Array ( ) [2] => Array ( ) [name] => Array ( ) [3] => Array ( ) [race] => Array ( ) [4] => Array ( ) [zone] => Array ( ) [5] => Array ( ) ) Array ( [0] => Array ( ) [level] => Array ( ) [1] => Array ( ) [class] => Array ( ) [2] => Array ( ) [name] => Array ( ) [3] => Array ( ) [race] => Array ( ) [4] => Array ( ) [zone] => Array ( ) [5] => Array ( ) ) Quote Link to comment https://forums.phpfreaks.com/topic/114169-solved-manipulating-string/#findComment-587348 Share on other sites More sharing options...
spider661 Posted July 11, 2008 Author Share Posted July 11, 2008 also wanted to note that $ret = '[72 Stone Fist] Yuari (Iksar) zone: gfaydark AccID: 298 AccName: Flora LSID: 110561 Status: 0 [72 Soothsayer] Kefus (Iksar) zone: qrg AccID: 307 AccName: Nerine LSID: 110954 Status: 0 [74 Wildblood] Tigger (Vah Shir) zone: gukbottom AccID: 244 AccName: harkenus2 LSID: 112355 Status: 0 3 players online'; is not always that.. it changes each time i call it but the format is the same doing the preg_match_all the way you posted it worked but when i loaded it with the ret from my code then i got that output above. i got it working the way i want above i just need to split the level class and take out annon if its there before the level it could look like ether/or and i need it to take out annon and split 75 and Arch Magus or whatever it may be into 2 diff strings i can use in the table [annon 75 Arch Magus] [75 Arch Magus] Quote Link to comment https://forums.phpfreaks.com/topic/114169-solved-manipulating-string/#findComment-587362 Share on other sites More sharing options...
DarkerAngel Posted July 11, 2008 Share Posted July 11, 2008 <?php $ret = "[annon 75 Arch Magus] Yuari (Iksar) zone: gfaydark AccID: 298 AccName: Flora LSID: 110561 Status: 0 [72 Soothsayer] Kefus (Iksar) zone: qrg AccID: 307 AccName: Nerine LSID: 110954 Status: 0 [74 Wildblood] Tigger (Vah Shir) zone: gukbottom AccID: 244 AccName: harkenus2 LSID: 112355 Status: 0 3 players online"; $array = explode("\n", $ret); $roster = array(); $i = 1; foreach ($array as $key => $value) { if(preg_match("#\[([0-9]+) ([a-zA-z ]+)\] ([a-zA-Z]+) \(([a-zA-Z ]+)\) zone: ([a-zA-Z]+)#", $value, $out) || preg_match("#\[[a-zA-Z]+ ([0-9]+) ([a-zA-z ]+)\] ([a-zA-Z]+) \(([a-zA-Z ]+)\) zone: ([a-zA-Z]+)#", $value, $out)) { $roster[$i] = array("name" => $out[3], "level" => $out[1], "class" => $out[3], "race" => $out[4], "zone" => $out[5]); $i++; } } echo("<pre>"); print_r($roster); echo("</pre>"); ?> It's a little bit over coded but it works with the results, Currently it will output: Array ( [1] => Array ( [name] => Yuari [level] => 75 [class] => Yuari [race] => Iksar [zone] => gfaydark ) [2] => Array ( [name] => Kefus [level] => 72 [class] => Kefus [race] => Iksar [zone] => qrg ) [3] => Array ( [name] => Tigger [level] => 74 [class] => Tigger [race] => Vah Shir [zone] => gukbottom ) ) Also it does the anomaly check in case of [annon 75 Arch Magus] Quote Link to comment https://forums.phpfreaks.com/topic/114169-solved-manipulating-string/#findComment-587382 Share on other sites More sharing options...
spider661 Posted July 11, 2008 Author Share Posted July 11, 2008 ok that looks good if i post it like it is above. but again if i put it to my code it does nothing. so maybe im doing something wrong this is my full page code. <?php echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" dir="{S_CONTENT_DIRECTION}" lang="{S_USER_LANG}" xml:lang="{S_USER_LANG}"> <head> <link rel="stylesheet" href="stylesheet.css" type="text/css" /> <!--[if lte IE 6]><script defer type="text/javascript" src="pngfix.js"></script><![endif]--> <!--[if IE ]><link rel="stylesheet" href="ie.css" type="text/css" /><![endif]--> </head> <body> <div id="container"> <div id="wrapper"> <div id="header"> <a href="http://scarsofamerous.com/"><img src="images/site_logo.png"></a> </div> <div id="content1"> <div id="content2"> <div id="content3"> <br><br><br><br><br><center><font color="#FF0000" size="4"><strong>Welcome to Scars of Amerous!</strong></font></center><br><br><br><br><br><hr class="hr">'; include "datas/config.php"; fConnect($fp,$Server,$Port,$user,$pass); fputs ($fp, "who\r"); usleep(125000); echo '<center><table border="4" cellpadding="4" cellspacing="4" ALIGN="CENTER"> <tr> <th>Name</th> <th>Level</th> <th>Race</th> <th>Class</th> <th>Zone</th> </tr>'; while (!feof($fp)) { $ret = fgets($fp); if (ereg("zone", $ret)) { $array = explode("\n", $ret); $roster = array(); $i = 1; foreach ($array as $key => $value) { if(preg_match("#\[([0-9]+) ([a-zA-z ]+)\] ([a-zA-Z]+) \(([a-zA-Z ]+)\) zone: ([a-zA-Z]+)#", $value, $out) || preg_match("#\[[a-zA-Z]+ ([0-9]+) ([a-zA-z ]+)\] ([a-zA-Z]+) \(([a-zA-Z ]+)\) zone: ([a-zA-Z]+)#", $value, $out)) { $roster[$i] = array("name" => $out[3], "level" => $out[1], "class" => $out[3], "race" => $out[4], "zone" => $out[5]); $i++; } } $name = $roster["name"]; $race = $roster["race"]; $level = $roster["level"]; $class = $roster["class"]; $zone = $roster["zone"]; //$name = substring_between($ret, '] ', ' ('); //$race = substring_between($ret, '(', ')'); //$level = substring_between($ret, '[', ']'); //$zone = substring_between($ret, 'zone: ', ' AccID:'); echo "<tr> <td class='smalltable'> $name </td> <td class='smalltable'> $level </td> <td class='smalltable'> $race </td> <td class='smalltable'> $class </td> <td class='smalltable'> $zone </td> </tr>"; } if (ereg("online", $ret)){ echo "$ret \n"; fclose($fp); break; } } echo '</table> <center> </div> </div> </div> <div id="footer"> <span class="notice"> © 2008 Spider661<br /></span> </div> </div> </div> </body> </html>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/114169-solved-manipulating-string/#findComment-587404 Share on other sites More sharing options...
spider661 Posted July 11, 2008 Author Share Posted July 11, 2008 ok for the life of me I cant figure out y but this works and does exactly what i wanted. is there a better way to do it though or should i just stick with whats working? while (!feof($fp)) { $ret = fgets($fp); if (ereg("zone", $ret)) { $array = explode("status:", $ret); $roster = array(); $i = 1; foreach ($array as $key => $value) { if(preg_match("#\[([0-9]+) ([a-zA-z ]+)\] ([a-zA-Z]+) \(([a-zA-Z ]+)\) zone: ([a-zA-Z]+)#", $value, $out) || preg_match("#\[[a-zA-Z]+ ([0-9]+) ([a-zA-z ]+)\] ([a-zA-Z]+) \(([a-zA-Z ]+)\) zone: ([a-zA-Z]+)#", $value, $out)) { //$roster[$i] = array("name" => $out[3], "level" => $out[1], "class" => $out[2], "race" => $out[4], "zone" => $out[5]); $i++; } } $name = $out[3]; $race = $out[4]; $level = $out[1]; $class = $out[2]; $zone = $out["5"]; it seems part of the problem was the explode /n is not there becuase there is no next line its all output on the same line. so i picked something closer to the end status: as for the array i really don't understand whats going on there so if someone would like to explain it i would love that but if not its cool.. anyways this is what i got. Quote Link to comment https://forums.phpfreaks.com/topic/114169-solved-manipulating-string/#findComment-587481 Share on other sites More sharing options...
spider661 Posted July 12, 2008 Author Share Posted July 12, 2008 i was given this on another forum that deals more with the server im trying to get the info from but there is a bu in it can someone see if they can find the problem. 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, -; } // Status if (preg_match("/Status:.*/", $value, $matches)) { $players[$key][status] = substr($matches[0], 8, 3); } } i called print_r($players); to try to list the array so i could see what the values where to post it all in my table and it gave an error for each player online that sayed Warning: Invalid argument supplied for foreach() in E:\wamp\www\who.php on line 46 line 46 is foreach ($ret as $key => $value){ Quote Link to comment https://forums.phpfreaks.com/topic/114169-solved-manipulating-string/#findComment-588530 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.