KingOfHeart Posted April 18, 2010 Share Posted April 18, 2010 I need to get some data off of a website. <!-- Generated by DeviBOT --> <!-- 0 = Normal --> <!-- 1 = Voiced --> <!-- 2 = Half-Op --> <!-- 3 = Op --> <!-- 4 = Admin --> <!-- 5 = Owner --> − <userlist> <count>7</count> − <user> <nick>Janus</nick> <prefix>5</prefix> </user> − <user> <nick>Dandel</nick> <prefix>4</prefix> </user> − <user> <nick>Zelda</nick> <prefix>4</prefix> </user> − <user> <nick>bayle</nick> <prefix>1</prefix> </user> − <user> <nick>Chris_Miller</nick> <prefix>1</prefix> </user> − <user> <nick>Deviance</nick> <prefix>1</prefix> </user> − <user> <nick>DeviBOT</nick> <prefix>1</prefix> </user> </userlist> I want to return the number of users, and the usernames. Then I want to draw a character depending on the prefix value. I'm thinking of using filegetcontents, and explode. Then I guess, use an array to replace the prefix number with a symbol. BTW the possible symbols are + % @ & ~ (regular gets no symbol, voice gets +, and so on). So how would you do this? Quote Link to comment https://forums.phpfreaks.com/topic/198893-extracting-data/ Share on other sites More sharing options...
Ken2k7 Posted April 18, 2010 Share Posted April 18, 2010 file_get_contents and preg_match_all should do get them for you. Quote Link to comment https://forums.phpfreaks.com/topic/198893-extracting-data/#findComment-1044035 Share on other sites More sharing options...
KingOfHeart Posted April 18, 2010 Author Share Posted April 18, 2010 I never understood preg at all. Even though I was linked to documents and such, I never got the hang of it. So can you help create that part of the script? Quote Link to comment https://forums.phpfreaks.com/topic/198893-extracting-data/#findComment-1044230 Share on other sites More sharing options...
Ken2k7 Posted April 19, 2010 Share Posted April 19, 2010 I never understood preg at all. Even though I was linked to documents and such, I never got the hang of it. So can you help create that part of the script? Sure. Can you post up what you attempted? It doesn't have to be correct, but something you tried. I can help you debug it so you understand how to use it next time. Quote Link to comment https://forums.phpfreaks.com/topic/198893-extracting-data/#findComment-1044400 Share on other sites More sharing options...
KingOfHeart Posted April 19, 2010 Author Share Posted April 19, 2010 <?php $inbox_text_only = true; include "global.php"; if(isset($inbox_msg)) { header("Content-type: image/png"); $im = imagecreatefrompng("images/msgbox.PNG"); $color = imagecolorallocate($im, 220, 0, 0);//0,0,0 imagestring($im, 4, 5, 3, $inbox_msg, $color); imagepng($im); imagedestroy($im); return; } $data = "http://stats.thegaminguniverse.nl/inchat.php?chan=oz"; $contents = file_get_contents($data); $str = explode("\n",$contents); $count = $str[0]; $users = $str[1]; $users = str_replace("&Zelda", "", $users); $users = str_replace(",","", $users); $ozcount = $count - 1;//ignore zelda //ignore MiniKouruu and StatsBot if(strpos($users, 'MiniKouruu')) { $users = str_replace(" @MiniKouruu", "", $users); $ozcount -= 1; } if(strpos($users, 'HickBawt')) { $users = str_replace(" @HickBawt", "", $users); $ozcount -= 1; } if(stripos($users, 'StatsBot')) { $users = str_replace(" +StatsBot", "", $users); $users = str_replace(" StatsBot", "", $users); $users = str_replace(" +Statsbot", "", $users); $users = str_replace(" Statsbot", "", $users); $users = str_replace(" +statsbot", "", $users); $users = str_replace(" statsbot", "", $users); $ozcount -= 1; } if($ozcount == -1) $ozcount = 0; $title = "Members in #OZ (" . $ozcount . ")"; if($count >= 2) { $advertise[0] = "Just ask anyone of the members with ~ or &"; $advertise[1] = "beside their name for some help with Open Zelda."; $advertise[2] = "Click on this image to join the #oz chat!"; } else { $advertise[0] = "Sorry there are currently no members in the OZ chat."; $advertise[1] = "at this time."; $advertise[2] = "Click on this image to join the #oz chat!"; } $users = wordwrap($users, 45, "*"); //edit this line if the users stretch too far or to little on image. $users = explode('*',$users, -1); header("Content-type: image/png"); if(isset($_GET['pressed'])) $im = imagecreatefrompng("images/inchat.png"); else $im = imagecreatefrompng("images/inchat.png"); $bg = imagecolorallocate($im, 255, 255, 255); $bg2 = imagecolorallocate($im, 200, 255, 200); $color = imagecolorallocate($im, 0, 0, 0);//0,0,0 $color2 = imagecolorallocate($im, 0, 0, 0);//0,0,0 $px = (imagesx($im) - 6.3 * strlen($title)) / 2; $px = (imagesx($im) - 5.9 * strlen($uv)) / 2; imagestring($im, 3, $px, 5 , $title, $color2); $px = (imagesx($im) - 7.8 * strlen($users[0])) / 2; //if(strlen($users[0]) > 0) //imagefilledrectangle ($im,$px,26, $px + strlen($users[0]) * 8, 40,$bg2); imagestring($im, 4, $px, 27 , $users[0], $color2); if($count < 2) { $px = (imagesx($im) - 6.3 * strlen($advertise[0])) / 2; imagestring($im, 2, $px, 34, $advertise[0], $color2); } //imagestring($im, 4, $px + 2, 27 , $users[0], $color); $px = (imagesx($im) - 7.8 * strlen($users[1])) / 2; //if(strlen($users[1]) > 0) // imagefilledrectangle ($im,$px,41, $px + strlen($users[1]) * 8, 55,$bg2); imagestring($im, 4, $px, 42 , $users[1], $color); $px = (imagesx($im) - 7.8 * strlen($users[2])) / 2; //if(strlen($users[2]) > 0) //imagefilledrectangle ($im,$px,56, $px + strlen($users[2]) * 8, 70,$bg2); imagestring($im, 4, $px, 57 , $users[2], $color); $px = (imagesx($im) - 7.8 * strlen($users[3])) / 2; //if(strlen($users[3]) > 0) // imagefilledrectangle ($im,$px,71, $px + strlen($users[3]) * 8, 85,$bg2); imagestring($im, 4, $px, 72 , $users[3], $color); $px = (imagesx($im) - 7.8 * strlen($users[4])) / 2; //if(strlen($users[4]) > 0) // imagefilledrectangle ($im,$px,86, $px + strlen($users[4]) * 8, 90,$bg2); imagestring($im, 4, $px, 87 , $users[4], $color); /*$px = (imagesx($im) - 6.3 * strlen($advertise[0])) / 2; imagestring($im, 2, $px, 95 , $advertise[0], $color2); $px = (imagesx($im) - 6.3 * strlen($advertise[1])) / 2; imagestring($im, 2, $px, 105 , $advertise[1], $color2); $px = (imagesx($im) - 6.3 * strlen($advertise[2])) / 2; imagestring($im, 2, $px, 130 , $advertise[2], $color2);*/ imagepng($im); imagedestroy($im); break; ?> That's my old code, but the site no longer updates. http://api.dproj.info/oz.xml I'm going to try this next, but not sure how to extract this data. Quote Link to comment https://forums.phpfreaks.com/topic/198893-extracting-data/#findComment-1044664 Share on other sites More sharing options...
KingOfHeart Posted May 31, 2010 Author Share Posted May 31, 2010 http://api.dproj.info/oz.xml So how do I extract this data? I need to extract the total into $count and the list of users into $users. Quote Link to comment https://forums.phpfreaks.com/topic/198893-extracting-data/#findComment-1065909 Share on other sites More sharing options...
Andy-H Posted June 1, 2010 Share Posted June 1, 2010 $path = 'http://api.dproj.info/oz.xml'; $modes[0] = ''; $modes[1] = '+'; $modes[2] = '%'; $modes[3] = '@'; $modes[4] = '&'; $modes[5] = '~'; $users = array(); try { if (!$data = simplexml_load_file($path)) { throw new Exception('Error reading file: ' . $path); } else { $inChannel = $data->count; foreach($data->user as $user) { $users[] = $modes[(int)$user->prefix] . $user->nick; } } echo 'User count: ' . $inChannel; echo '<br >------------<br >'; echo implode('<br >', $users); echo '<br >------------<br >'; } catch(Exception $e) { echo $e->getMessage(); } Quote Link to comment https://forums.phpfreaks.com/topic/198893-extracting-data/#findComment-1065946 Share on other sites More sharing options...
KingOfHeart Posted June 1, 2010 Author Share Posted June 1, 2010 Interesting script. I'll try modifying it to the way I need it. In the mean time, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/198893-extracting-data/#findComment-1065974 Share on other sites More sharing options...
Andy-H Posted June 1, 2010 Share Posted June 1, 2010 No worries, for the record, I didn't really know what I was doing. I used http://www.devshed.com/c/a/PHP/Introducing-SimpleXML-in-PHP-5/1/ And some simple debugging techniques such as var_dump and print_r to help me understand the data that was returned by simplexml_load_file. Since your editing the code to fit your needs, this stuff will probably be usefull to you too. Quote Link to comment https://forums.phpfreaks.com/topic/198893-extracting-data/#findComment-1066137 Share on other sites More sharing options...
KingOfHeart Posted June 2, 2010 Author Share Posted June 2, 2010 Well, I got close but hit an error. <?php function FindUsers() { global $count, $mem, $path; if (!$data = simplexml_load_file($path)) return false; else { $inChannel = $data->count; foreach($data->user as $user) { $mem .= $modes[(int)$user->prefix] . $user->nick; } } $count = $inChannel; return true; } $inbox_text_only = true; include "global.php"; if(isset($inbox_msg)) { header("Content-type: image/png"); $im = imagecreatefrompng("images/msgbox.PNG"); $color = imagecolorallocate($im, 220, 0, 0);//0,0,0 imagestring($im, 4, 5, 3, $inbox_msg, $color); imagepng($im); imagedestroy($im); return; } $path = 'http://api.dproj.info/oz.xml'; $modes[0] = ''; $modes[1] = '+'; $modes[2] = '%'; $modes[3] = '@'; $modes[4] = '&'; $modes[5] = '~'; $users; $count = 0; $stats = FindUsers(); if($stats == false) return false; $title = "Members in #OZ (" . $count . ")"; if($count >= 2) { $advertise[0] = "Just ask anyone of the members with ~ or &"; $advertise[1] = "beside their name for some help with Open Zelda."; $advertise[2] = "Click on this image to join the #oz chat!"; } else { $advertise[0] = "Sorry there are currently no members in the OZ chat."; $advertise[1] = "at this time."; $advertise[2] = "Click on this image to join the #oz chat!"; } $users = wordwrap($mem, 45, "*"); //edit this line if the users stretch too far or to little on image. $users = implode("*",$mem); echo $users; return; header("Content-type: image/png"); if(isset($_GET['pressed'])) $im = imagecreatefrompng("images/inchat.png"); else $im = imagecreatefrompng("images/inchat.png"); $bg = imagecolorallocate($im, 255, 255, 255); $bg2 = imagecolorallocate($im, 200, 255, 200); $color = imagecolorallocate($im, 0, 0, 0);//0,0,0 $color2 = imagecolorallocate($im, 0, 0, 0);//0,0,0 $px = (imagesx($im) - 6.3 * strlen($title)) / 2; $px = (imagesx($im) - 5.9 * strlen($uv)) / 2; imagestring($im, 3, $px, 5 , $title, $color2); $px = (imagesx($im) - 7.8 * strlen($users[0])) / 2; //if(strlen($users[0]) > 0) //imagefilledrectangle ($im,$px,26, $px + strlen($users[0]) * 8, 40,$bg2); imagestring($im, 4, $px, 27 , $users[0], $color2); if($count < 2) { $px = (imagesx($im) - 6.3 * strlen($advertise[0])) / 2; imagestring($im, 2, $px, 34, $advertise[0], $color2); } //imagestring($im, 4, $px + 2, 27 , $users[0], $color); $px = (imagesx($im) - 7.8 * strlen($users[1])) / 2; //if(strlen($users[1]) > 0) // imagefilledrectangle ($im,$px,41, $px + strlen($users[1]) * 8, 55,$bg2); imagestring($im, 4, $px, 42 , $users[1], $color); $px = (imagesx($im) - 7.8 * strlen($users[2])) / 2; //if(strlen($users[2]) > 0) //imagefilledrectangle ($im,$px,56, $px + strlen($users[2]) * 8, 70,$bg2); imagestring($im, 4, $px, 57 , $users[2], $color); $px = (imagesx($im) - 7.8 * strlen($users[3])) / 2; //if(strlen($users[3]) > 0) // imagefilledrectangle ($im,$px,71, $px + strlen($users[3]) * 8, 85,$bg2); imagestring($im, 4, $px, 72 , $users[3], $color); $px = (imagesx($im) - 7.8 * strlen($users[4])) / 2; //if(strlen($users[4]) > 0) // imagefilledrectangle ($im,$px,86, $px + strlen($users[4]) * 8, 90,$bg2); imagestring($im, 4, $px, 87 , $users[4], $color); /*$px = (imagesx($im) - 6.3 * strlen($advertise[0])) / 2; imagestring($im, 2, $px, 95 , $advertise[0], $color2); $px = (imagesx($im) - 6.3 * strlen($advertise[1])) / 2; imagestring($im, 2, $px, 105 , $advertise[1], $color2); $px = (imagesx($im) - 6.3 * strlen($advertise[2])) / 2; imagestring($im, 2, $px, 130 , $advertise[2], $color2);*/ imagepng($im); imagedestroy($im); break; ?> Warning: implode() [function.implode]: Invalid arguments passed in /home/thegamin/public_html/openzelda/users2.php on line 69 Explode, implode it don't matter. What I need is to use wordwrap to separate users into another array if it gets too long. http://openzelda.thegaminguniverse.com/users2.php Quote Link to comment https://forums.phpfreaks.com/topic/198893-extracting-data/#findComment-1066512 Share on other sites More sharing options...
Andy-H Posted June 2, 2010 Share Posted June 2, 2010 <?php function FindUsers() { global $count, $mem, $path; if (!$data = simplexml_load_file($path)) return false; else { $modes[0] = ''; $modes[1] = '+'; $modes[2] = '%'; $modes[3] = '@'; $modes[4] = '&'; $modes[5] = '~'; $count = $data->count; foreach($data->user as $user) { $curUser = $modes[(int)$user->prefix] . $user->nick; $mem[] = wordwrap($curUser, 45, "*"); } } return true; } $inbox_text_only = true; include "global.php"; if(isset($inbox_msg)) { header("Content-type: image/png"); $im = imagecreatefrompng("images/msgbox.PNG"); $color = imagecolorallocate($im, 220, 0, 0);//0,0,0 imagestring($im, 4, 5, 3, $inbox_msg, $color); imagepng($im); imagedestroy($im); return; } $path = 'http://api.dproj.info/oz.xml'; $count = 0; $stats = FindUsers(); if($stats == false) return false; $title = "Members in #OZ (" . $count . ")"; if($count >= 2) { $advertise[0] = "Just ask anyone of the members with ~ or &"; $advertise[1] = "beside their name for some help with Open Zelda."; $advertise[2] = "Click on this image to join the #oz chat!"; } else { $advertise[0] = "Sorry there are currently no members in the OZ chat."; $advertise[1] = "at this time."; $advertise[2] = "Click on this image to join the #oz chat!"; } $users = implode('<br >', $mem); echo $users; return; header("Content-type: image/png"); if(isset($_GET['pressed'])) $im = imagecreatefrompng("images/inchat.png"); else $im = imagecreatefrompng("images/inchat.png"); $bg = imagecolorallocate($im, 255, 255, 255); $bg2 = imagecolorallocate($im, 200, 255, 200); $color = imagecolorallocate($im, 0, 0, 0);//0,0,0 $color2 = imagecolorallocate($im, 0, 0, 0);//0,0,0 $px = (imagesx($im) - 6.3 * strlen($title)) / 2; $px = (imagesx($im) - 5.9 * strlen($uv)) / 2; imagestring($im, 3, $px, 5 , $title, $color2); $px = (imagesx($im) - 7.8 * strlen($users[0])) / 2; //if(strlen($users[0]) > 0) //imagefilledrectangle ($im,$px,26, $px + strlen($users[0]) * 8, 40,$bg2); imagestring($im, 4, $px, 27 , $users[0], $color2); if($count < 2) { $px = (imagesx($im) - 6.3 * strlen($advertise[0])) / 2; imagestring($im, 2, $px, 34, $advertise[0], $color2); } //imagestring($im, 4, $px + 2, 27 , $users[0], $color); $px = (imagesx($im) - 7.8 * strlen($users[1])) / 2; //if(strlen($users[1]) > 0) // imagefilledrectangle ($im,$px,41, $px + strlen($users[1]) * 8, 55,$bg2); imagestring($im, 4, $px, 42 , $users[1], $color); $px = (imagesx($im) - 7.8 * strlen($users[2])) / 2; //if(strlen($users[2]) > 0) //imagefilledrectangle ($im,$px,56, $px + strlen($users[2]) * 8, 70,$bg2); imagestring($im, 4, $px, 57 , $users[2], $color); $px = (imagesx($im) - 7.8 * strlen($users[3])) / 2; //if(strlen($users[3]) > 0) // imagefilledrectangle ($im,$px,71, $px + strlen($users[3]) * 8, 85,$bg2); imagestring($im, 4, $px, 72 , $users[3], $color); $px = (imagesx($im) - 7.8 * strlen($users[4])) / 2; //if(strlen($users[4]) > 0) // imagefilledrectangle ($im,$px,86, $px + strlen($users[4]) * 8, 90,$bg2); imagestring($im, 4, $px, 87 , $users[4], $color); /*$px = (imagesx($im) - 6.3 * strlen($advertise[0])) / 2; imagestring($im, 2, $px, 95 , $advertise[0], $color2); $px = (imagesx($im) - 6.3 * strlen($advertise[1])) / 2; imagestring($im, 2, $px, 105 , $advertise[1], $color2); $px = (imagesx($im) - 6.3 * strlen($advertise[2])) / 2; imagestring($im, 2, $px, 130 , $advertise[2], $color2);*/ imagepng($im); imagedestroy($im); break; ?> Quote Link to comment https://forums.phpfreaks.com/topic/198893-extracting-data/#findComment-1066748 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.