nicse4 Posted May 24, 2012 Share Posted May 24, 2012 Can someone help me with this? I have a table containing 649 rows like the following: <tr> <td class="numeric"><span class="pki" rel="pkiAll n166"></span> 166</td> <td class="name"><a href="http://pokemondb.net/pokedex/ledian" title="View pokedex for #166 Ledian">Ledian</a></td> <td class="cell-type"><a class="type bug" href="http://pokemondb.net/type/bug">Bug</a><br> <a class="type flying" href="http://pokemondb.net/type/flying">Flying</a></td> <td>55</td> <td>35</td> <td>50</td> <td>55</td> <td>110</td> <td>85</td> <td class="total">390</td> </tr> I want to make that into an array as follows: $blah[166] = "Ledian"; $blah2[166] = 1; etc for all 649 rows. can someone tell me how to do this with a regex or some other way? Thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/ Share on other sites More sharing options...
nicse4 Posted May 24, 2012 Author Share Posted May 24, 2012 Can someone help me with this? I have a table containing 649 rows like the following: <tr> <td class="numeric"><span class="pki" rel="pkiAll n166"></span> 166</td> <td class="name"><a href="http://pokemondb.net/pokedex/ledian" title="View pokedex for #166 Ledian">Ledian</a></td> <td class="cell-type"><a class="type bug" href="http://pokemondb.net/type/bug">Bug</a><br> <a class="type flying" href="http://pokemondb.net/type/flying">Flying</a></td> <td>55</td> <td>35</td> <td>50</td> <td>55</td> <td>110</td> <td>85</td> <td class="total">390</td> </tr> I want to make that into an array as follows: $blah[166] = "Ledian"; $blah2[166] = 1; etc for all 649 rows. can someone tell me how to do this with a regex or some other way? Thanks a lot. sorry about the double post. no edit button on top post. forgot to mention I only need the info that is not striked out bellow. <tr> <td class="numeric"><span class="pki" rel="pkiAll n166"></span>166</td> <td class="name"><a href="http://pokemondb.net/pokedex/ledian" title="View pokedex for #166 Ledian">Ledian</a></td> <td class="cell-type"><a class="type bug" href="http://pokemondb.net/type/bug">Bug</a><br> <a class="type flying" href="http://pokemondb.net/type/flying">Flying</a></td> <td>55</td> <td>35</td> <td>50</td> <td>55</td> <td>110</td> <td>85</td> <td class="total">390</td> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/#findComment-1348178 Share on other sites More sharing options...
Kays Posted May 24, 2012 Share Posted May 24, 2012 Err... it's possible ... but why do you have 649 rows of that? I'm assuming that there are currently 649 number of Pokémon, but where did you get the HTML for all of them? Was there a generator? Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/#findComment-1348193 Share on other sites More sharing options...
BarryT06 Posted May 24, 2012 Share Posted May 24, 2012 Hi, Not entirely sure how your getting the data, as in if you are getting a feed of it, or whether you type it out yourself. I think the best way to do that is input the data into a DB or Array, and build the table from the DB or Array. Barry Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/#findComment-1348196 Share on other sites More sharing options...
nicse4 Posted May 24, 2012 Author Share Posted May 24, 2012 @kays: there are 649 pokemon currently @Barry: Thats where the issue arrises. I want to put it all into a database, but I don't have the time to manually type each row into an SQL, and I can't get dreamweaver to format it correctly, otherwise this would be done already. My question is is how do I get php to sort out the data, then put it into the database. Thanks if you can help me with this. Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/#findComment-1348390 Share on other sites More sharing options...
Barand Posted May 24, 2012 Share Posted May 24, 2012 but where did you get the HTML for all of them? Was there a generator? Not entirely sure how your getting the data, as in if you are getting a feed of it Answers to these questions may help Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/#findComment-1348443 Share on other sites More sharing options...
BarryT06 Posted May 25, 2012 Share Posted May 25, 2012 If you have the information in a file you could format it and then import it to your MySQl DB. Once in there u can build your table from it, or populate an array. Importing will allow u take that buig chunk of data and save it in the DB without having to manually type a load of stuff Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/#findComment-1348613 Share on other sites More sharing options...
nicse4 Posted May 25, 2012 Author Share Posted May 25, 2012 I got the source from Pokemondb.net, and my issue is Dreamweaver wont format it into the way I want it to, else there wouldn't be a problem, I need help making a PHP regex to get the data out of the table and into a database. Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/#findComment-1348687 Share on other sites More sharing options...
Barand Posted May 25, 2012 Share Posted May 25, 2012 Can you get it as an XML file? Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/#findComment-1348688 Share on other sites More sharing options...
BarryT06 Posted May 26, 2012 Share Posted May 26, 2012 Can you post a sample of the source in its current format? Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/#findComment-1348696 Share on other sites More sharing options...
nicse4 Posted May 26, 2012 Author Share Posted May 26, 2012 can't get it as an xml, but can probably save it as tr's and td's in an xml. probably. I will see. And what do you mean? like right from pokemondb or as i have it now? Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/#findComment-1348702 Share on other sites More sharing options...
BarryT06 Posted May 26, 2012 Share Posted May 26, 2012 either or both, just want to see the format it comes in, to see if i can automate it. Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/#findComment-1348712 Share on other sites More sharing options...
nicse4 Posted May 26, 2012 Author Share Posted May 26, 2012 the attached file has only the part I need info from. if you want all of the source the link is: http://pokemondb.net/pokedex/all 18462_.php Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/#findComment-1348777 Share on other sites More sharing options...
BarryT06 Posted May 26, 2012 Share Posted May 26, 2012 OK, so did you just go to that page, view source and copy the table contents? I thought they provided you with a source file. If this is the only way you can get the data, I'm not sure I can be of much help. I suppose you could use find and replace to clear out all the table tags and then you would be left with raw data but you would have to comma seperate everything. If you did that you could then import to MySQL through PHP. Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/#findComment-1348786 Share on other sites More sharing options...
BarryT06 Posted May 26, 2012 Share Posted May 26, 2012 If you want to be able to import this into your DB you are going to need to format the file. I have made a start and done a lot of it, you will need to clear the <span class="pki" rel="pkiAll n7"></span> and the <a href="http://pokemondb.net/pokedex/squirtle" title="View pokedex for #007 Squirtle"> follow the format I have for the first 6 and you should be able to import through phpMyAdmin or through a PHP page using mysql_query and LOAD DATA command. Barry 18463_.php Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/#findComment-1348796 Share on other sites More sharing options...
Barand Posted May 26, 2012 Share Posted May 26, 2012 I took the section of table between <tbody> and </tbody> and processed it as an xml file (attached - poke1.txt) then ran this code to create a csv file which you can load into a db table (attached - pokemon.txt) <?php $xml = simplexml_load_file('poke1.xml'); $fp = fopen('pokemon.csv', 'w'); foreach ($xml->tr as $row) { $r = array(); foreach ($row->td as $cell) { if ($cell->a) //if an <a> tag, use that $r[] = (string)$cell->a; else $r[] = (string)$cell; } fputcsv($fp, $r); } fclose ($fp); ?> 18465_.txt 18466_.txt Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/#findComment-1348837 Share on other sites More sharing options...
BarryT06 Posted May 26, 2012 Share Posted May 26, 2012 @Barand Hey, could u explain a little about how that works? I wasnt aware you could do that, then again Im not exactly an expert, just someone wanting to learn more. @nicse4 you might need to check the type though before using that file, as some of the 'type' field info has 1 piece of info and others have two. ie. fire, ground, or Water Poison, etc and you may need to keep these in the same DB field. Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/#findComment-1348853 Share on other sites More sharing options...
Barand Posted May 26, 2012 Share Posted May 26, 2012 The html structure is almost identical to xml structure. (I had to strip out <br> tags to make it compatible). Have a look at simpleXML in manual to see how it works Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/#findComment-1348858 Share on other sites More sharing options...
BarryT06 Posted May 26, 2012 Share Posted May 26, 2012 Thanks, will do Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/#findComment-1348862 Share on other sites More sharing options...
BarryT06 Posted May 27, 2012 Share Posted May 27, 2012 Cool. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/263024-help-extract-data-from-html-table-and-put-into-a-php-array/#findComment-1348873 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.