Parsing HTML

SpaceshipSpaceship
Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
Watch

BeKustom

Established Member
Impact
3
I am looking for some advice on how to parse table data from a HTML file and store it in a mySQL database. The HTML file has 4 different tables that I need info from. Any help on this would be greatly appreciated. I can provide more detailed information if needed.
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
What are those tables?
Please give an example :tu:
 
0
Close
Alright here is an example of the tables. They are all laid out the same way as this one just with different data in the td tags. The code below isn't the entire table just a portion of it, hopefully you get the idea.

HTML:
<TABLE border="2"
	style="background : #173D54"
	cellpadding="3"
	cellspacing="2"
	summary="">
<TR	style="color : black">
<TD style="background : #CEDAEB">
	P
</TD>
<TD style="background : #CEDAEB">
	#
</TD>
<TD style="background : #CEDAEB">
	DRIVER
</TD>
<TD style="background : #CEDAEB">
	TIME
</TD>
</TR>
<TR	style="color : black">
<TD style="background : #CEDAEB">
	1
</TD>
<TD style="background : #CEDAEB">
	48
</TD>
<TD style="background : #CEDAEB">
	t henry
</TD>
<TD style="background : #CEDAEB">
	--
</TD>
<TR	style="color : black">
<TD style="background : #CEDAEB">
	2
</TD>
<TD style="background : #CEDAEB">
	38
</TD>
<TD style="background : #CEDAEB">
	B Elder
</TD>
<TD style="background : #CEDAEB">
	--
</TD>
<TR	style="color : black">
<TD style="background : #CEDAEB">
	3
</TD>
<TD style="background : #CEDAEB">
	25
</TD>
<TD style="background : #CEDAEB">
	J Ahlin
</TD>
<TD style="background : #CEDAEB">
	--
</TD>
<TR	style="color : black">
<TD style="background : #CEDAEB">
	4
</TD>
 
0
Close
Please try this :)
It works here..

PHP:
<?php
$a = file('yourfile.html');
$a = implode($a, "\r\n");

preg_match_all('/<td style=\"([a-z0-9\:\;\# ]+)\">(.*)?<\/td>/i', $a, $match);

echo('<pre>'.print_r($match[2], true).'</pre>');
?>

With your example, it displays :
PHP:
Array
(
    [0] =>  P 
    [1] =>  # 
    [2] =>  DRIVER 
    [3] =>  TIME 
    [4] =>  1 
    [5] =>  48 
    [6] =>  t henry 
    [7] =>  -- 
    [8] =>  2 
    [9] =>  38 
    [10] =>  B Elder 
    [11] =>  -- 
    [12] =>  3 
    [13] =>  25 
    [14] =>  J Ahlin 
    [15] =>  -- 
    [16] =>  4 
)
 
0
Close
Thanks man. That works but now my question is how can I pull the data from the other 3 tables in the same file?
 
0
Close
BeKustom said:
Thanks man. That works but now my question is how can I pull the data from the other 3 tables in the same file?
PHP:
<table>
   <!-- first data -->
</table>
<table>
   <!-- second data -->
</table>
<table>
   <!-- third data -->
</table>
Is there any unique string on each table ?
For example unique HTML style which does not applied in other tables or such?
 
0
Close
No there isn't and I sent you a PM. That was my biggest problem before when I attempted this for about 2 weeks straight. I know it can be done because a few sites related to this game do it.
 
0
Close
If the server you are using supports PHP 5 and has the XML extension SimpleXML installed then use it. You have to just add this line at the top of you html file to make it as XHTML

Code:
<?xml version="1.0" encoding="UTF-8" ?>

then use simplexml to get tagwise data. see the manual of simplexml here- www.php.net/simplexml

it will be really useful if you do this way. :)
 
0
Close
you could just loop through the file like this:
PHP:
foreach($file as $line) {
   if(strpos($line, '<table>') {
     // save this data to an array
   }
   if(strpos($line, '</table>') {
     // iterate to the next array 
   }
}
then loop you new array with the preg match that was given above...
 
0
Close
AIVikingsAIVikings

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back