Jump to content

Help: Extract data from HTML table and put into a PHP array


nicse4

Recommended Posts

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.