php_b34st Posted July 23, 2006 Share Posted July 23, 2006 Hi I have the following code which grabs a table from a website[code]<?php$user = 'devil may6';function RSstats($url){$rssstring = file_get_contents($url);preg_match_all('#<table cellpadding="3" cellspacing="0" border=0>(.*?)</table>#s', $rssstring, $stat); $score = $stat[1][0];$score = strip_tags($score);echo $score;}RSstats('http://hiscore.runescape.com/lang/en/aff/runescape/hiscorepersonal.ws?user1=' . $user);?>[/code]and displays it like this[quote]Personal scores for Devil May6 Skill RankLevelXP Overall 999,998 667 1,597,232 Attack Not Ranked Defence 529,783 57 214,857 Strength Not Ranked Hitpoints 932,293 55 172,561 Ranged Not Ranked Prayer 325,757 46 68,243 Magic 281,017 62 349,834 Cooking Not Ranked Woodcutting 432,602 65 465,040 Fletching 565,357 38 30,988 Fishing Not Ranked Firemaking Not Ranked Crafting Not Ranked Smithing Not Ranked Mining Not Ranked Herblore 219,323 34 21,909 Agility Not Ranked Thieving Not Ranked Slayer Not Ranked Farming Not Ranked Runecraft Not Ranked Construction Not Ranked[/quote]I would like this information put into an array like this:[quote]Array( [0] => Array ( [0] => Overall [1] => 999,998 [2] => 667 [3] => 1,597,232 ) [1] => Array ( [0] => Attack [1] => Not Ranked ) [2] => Array ( [0] => Defence [1] => 529,783 [2] => 57 [3] => 214,857 )etc...[/quote]But I dont know how i would go about this could anyone help please? Quote Link to comment https://forums.phpfreaks.com/topic/15385-arrays/ Share on other sites More sharing options...
Caesar Posted July 23, 2006 Share Posted July 23, 2006 Well, if we're talking simply putting into an array, the very easiest way to put it all into an array would be:[code]<?php$score = explode(" ", $score);?>[/code]You can then work with that array at that point. Of course you can get much more detailed. Quote Link to comment https://forums.phpfreaks.com/topic/15385-arrays/#findComment-62474 Share on other sites More sharing options...
php_b34st Posted July 23, 2006 Author Share Posted July 23, 2006 How do i use it once it is in an array? i tried:[code]echo $score[0];echo $score[1];[/code]but nothing seemed to happen? Quote Link to comment https://forums.phpfreaks.com/topic/15385-arrays/#findComment-62482 Share on other sites More sharing options...
Barand Posted July 23, 2006 Share Posted July 23, 2006 Try this[code]<?php$txt = "Personal scores for Devil May6 Skill RankLevelXP Overall 999,998 667 1,597,232 Attack Not Ranked Defence 529,783 57 214,857 Strength Not Ranked Hitpoints 932,293 55 172,561 Ranged Not Ranked Prayer 325,757 46 68,243 Magic 281,017 62 349,834 Cooking Not Ranked Woodcutting 432,602 65 465,040 Fletching 565,357 38 30,988 Fishing Not Ranked Firemaking Not Ranked Crafting Not Ranked Smithing Not Ranked Mining Not Ranked Herblore 219,323 34 21,909 Agility Not Ranked Thieving Not Ranked Slayer Not Ranked Farming Not Ranked Runecraft Not Ranked Construction Not Ranked";$cats = array('Overall', 'Attack', 'Defence', 'Strength', 'Hitpoints', 'Ranged', 'Prayer', 'Magic', 'Cooking', 'Woodcutting', 'Fletching', 'Fishing', 'Firemaking', 'Crafting', 'Smithing', 'Mining', 'Herblore', 'Agility', 'Thieving', 'Slayer', 'Farming', 'Runecraft', 'Construction');$score = array(); $data = explode (' ', $txt);$key = 'header';foreach ($data as $word) { if (in_array($word, $cats)) { $score[$word] = array(); $key = $word; } elseif ($word=='Not') { $score[$key][] = 'Not ranked'; } elseif ($word=='Ranked') { continue; } else $score[$key][] = $word;}// view the resultsecho '<pre>', print_r($score, true), '</pre>';?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15385-arrays/#findComment-62489 Share on other sites More sharing options...
php_b34st Posted July 23, 2006 Author Share Posted July 23, 2006 Yes that is how i wanted the data splitting up but the problem there is you have manually defined the txt to be put into an array, that txt will change and thats y i need to get it using file(). is there a way to do that after getting the data from the website rather than manually defining $txt? i tried changing $txt so to $txt = $score; but that messed it up. Quote Link to comment https://forums.phpfreaks.com/topic/15385-arrays/#findComment-62494 Share on other sites More sharing options...
Barand Posted July 23, 2006 Share Posted July 23, 2006 Of course I manually defined text. I had to have something to process so I used your quoted output.Substitute the text you get from the site. Quote Link to comment https://forums.phpfreaks.com/topic/15385-arrays/#findComment-62508 Share on other sites More sharing options...
Barand Posted July 23, 2006 Share Posted July 23, 2006 try this[code]$user = 'devil may6';function RSstats($url){$rssstring = file_get_contents($url);preg_match_all('#<table cellpadding="3" cellspacing="0" border=0>(.*?)</table>#s', $rssstring, $stat); $score = $stat[1][0];$score = strip_tags($score);return $score;}$txt = RSstats('http://hiscore.runescape.com/lang/en/aff/runescape/hiscorepersonal.ws?user1=' . $user);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15385-arrays/#findComment-62511 Share on other sites More sharing options...
php_b34st Posted July 23, 2006 Author Share Posted July 23, 2006 Thank you thats great exactly what i need. Quote Link to comment https://forums.phpfreaks.com/topic/15385-arrays/#findComment-62519 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.