NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Parsing HTML

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 01-26-2008, 12:22 AM THREAD STARTER               #1 (permalink)
NamePros Member
Join Date: Apr 2007
Location: South Carolina
Posts: 42
BeKustom is on a distinguished road
 



Parsing HTML


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.
__________________
InsaneCreationz
BeKustom is offline  
Old 01-26-2008, 12:58 AM   #2 (permalink)
i love automation
 
xrvel's Avatar
Join Date: Nov 2007
Location: xrvel.com
Posts: 1,620
xrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant future
 




What are those tables?
Please give an example
__________________
xrvel is offline  
Old 01-26-2008, 01:02 AM THREAD STARTER               #3 (permalink)
NamePros Member
Join Date: Apr 2007
Location: South Carolina
Posts: 42
BeKustom is on a distinguished road
 



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 Code:
<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>
__________________
InsaneCreationz
BeKustom is offline  
Old 01-26-2008, 01:46 AM   #4 (permalink)
i love automation
 
xrvel's Avatar
Join Date: Nov 2007
Location: xrvel.com
Posts: 1,620
xrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant future
 




Please try this
It works here..

PHP Code:
<?php
????: NamePros.com http://www.namepros.com/programming/423681-parsing-html.html
$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 Code:
Array
(
    [
0] =>  
    
[1] =>  
    
[2] =>  DRIVER 
    
[3] =>  TIME 
    
[4] =>  
    
[5] =>  48 
    
[6] =>  t henry 
????: NamePros.com http://www.namepros.com/showthread.php?t=423681
    
[7] =>  -- 
    [
8] =>  
    
[9] =>  38 
    
[10] =>  B Elder 
    
[11] =>  -- 
    [
12] =>  
    
[13] =>  25 
    
[14] =>  J Ahlin 
    
[15] =>  -- 
    [
16] =>  

__________________
xrvel is offline  
Old 01-26-2008, 01:59 AM THREAD STARTER               #5 (permalink)
NamePros Member
Join Date: Apr 2007
Location: South Carolina
Posts: 42
BeKustom is on a distinguished road
 



Thanks man. That works but now my question is how can I pull the data from the other 3 tables in the same file?
__________________
InsaneCreationz
BeKustom is offline  
Old 01-26-2008, 02:13 AM   #6 (permalink)
i love automation
 
xrvel's Avatar
Join Date: Nov 2007
Location: xrvel.com
Posts: 1,620
xrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant future
 





Originally Posted by BeKustom
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 Code:
<table>
????: NamePros.com http://www.namepros.com/showthread.php?t=423681
   <!-- 
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?
__________________
xrvel is offline  
Old 01-26-2008, 02:16 AM THREAD STARTER               #7 (permalink)
NamePros Member
Join Date: Apr 2007
Location: South Carolina
Posts: 42
BeKustom is on a distinguished road
 



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.
__________________
InsaneCreationz
BeKustom is offline  
Old 01-29-2008, 10:24 AM   #8 (permalink)
New Member
Join Date: Dec 2007
Location: Pune, Maharashtra, India
Posts: 11
itech7.com is an unknown quantity at this point
 



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
????: NamePros.com http://www.namepros.com/showthread.php?t=423681

it will be really useful if you do this way.
__________________
Nilesh

www.itech7.com
itech7.com is offline  
Old 02-14-2008, 05:56 PM   #9 (permalink)
New Member
Join Date: Feb 2008
Posts: 9
jimmysanders is an unknown quantity at this point
 



you could just loop through the file like this:
PHP Code:
foreach($file as $line) {
????: NamePros.com http://www.namepros.com/showthread.php?t=423681
   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...
jimmysanders is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 12:00 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger