Jump to content

Retrieving HTML data from a table


unneutral

Recommended Posts

Hi guys,

 

I've had a read of regex but I find it hard to understand most of it.

 

Basically, what I would like to do is to have a code that looks through a html table and grabs the data, storing them in separate strings to be inserted into a mysql database. An example of the table is:

 

<table><tr bgcolor=#000000><td colspan=2>
<b>Personal Information</b></td></tr>
<tr bgcolor=#202020><td width=20%>Name:</td><td>Jacob McGrath</td></tr>
<tr bgcolor=#ffffff><td>Gender:</td><td>male</td></tr>
<tr bgcolor=#202020><td>E-mail:</td><td>myemail@yourdomain.com</td></tr>
<tr bgcolor=#ffffff><td>Country:</td><td>Ireland</td></tr>
<tr bgcolor=#202020><td>Age:</td><td>23</td></tr>
<tr bgcolor=#ffffff><td>Degree:</td><td>Bachelor of Engineering</td></tr>
<tr bgcolor=#202020><td>Company:</td><td>McGrath's Engineering</td></tr>
<tr bgcolor=#ffffff><td>Link:</td><td><a href="http://www.yourdomain.com/mcgrath">McGrath</a></td></tr>
<tr bgcolor=#202020><TD>TimeZone:</td><td>GMT +3.00</td></tr>
</table>

 

Notice that the bgcolor alternates between #ffffff and #202020, when one of the information above is not given, the entire <tr></tr> will not be displayed, and the colours still alternate. For example, if we were to take out the "degree" data,

 

<table><tr bgcolor=#000000><td colspan=2>
<b>Personal Information</b></td></tr>
<tr bgcolor=#202020><td width=20%>Name:</td><td>Jacob McGrath</td></tr>
<tr bgcolor=#ffffff><td>Gender:</td><td>male</td></tr>
<tr bgcolor=#202020><td>E-mail:</td><td>myemail@yourdomain.com</td></tr>
<tr bgcolor=#ffffff><td>Country:</td><td>Ireland</td></tr>
<tr bgcolor=#202020><td>Age:</td><td>23</td></tr>
<tr bgcolor=#ffffff><td>Company:</td><td>McGrath's Engineering</td></tr>
<tr bgcolor=#202020><td>Link:</td><td><a href="http://www.yourdomain.com/mcgrath">McGrath</a></td></tr>
<tr bgcolor=#ffffff><TD>TimeZone:</td><td>GMT +3.00</td></tr>
</table>

 

I would like to be able to save all the info into their respective strings (Jacob Mcgrath to $name, Male to $gender, Ireland to $country, etc)

 

Thanks in advance :)

Link to comment
Share on other sites

<pre>
<?php

$html = <<<HTML
<table><tr bgcolor=#000000><td colspan=2>
<b>Personal Information</b></td></tr>
<tr bgcolor=#202020><td width=20%>Name:</td><td>Jacob McGrath</td></tr>
<tr bgcolor=#ffffff><td>Gender:</td><td>male</td></tr>
<tr bgcolor=#202020><td>E-mail:</td><td>myemail@yourdomain.com</td></tr>
<tr bgcolor=#ffffff><td>Country:</td><td>Ireland</td></tr>
<tr bgcolor=#202020><td>Age:</td><td>23</td></tr>
<tr bgcolor=#ffffff><td>Degree:</td><td>Bachelor of Engineering</td></tr>
<tr bgcolor=#202020><td>Company:</td><td>McGrath's Engineering</td></tr>
<tr bgcolor=#ffffff><td>Link:</td><td><a href="http://www.yourdomain.com/mcgrath">McGrath</a></td></tr>
<tr bgcolor=#202020><TD>TimeZone:</td><td>GMT +3.00</td></tr>
</table>
HTML;

preg_match_all('%<td.*?>((??!</td>).)+)</td><td.*?>((??!</td>).)+)</td>%', $html, $matches);
### Toss full matches.
array_shift($matches);
### Create associative array.
$data = array();
$index = 0;
foreach ($matches[0] as $key) {
	$key = str_replace(':', '', $key);
	$data[$key] = $matches[1][$index];
	++$index;
}
print_r($data);
?>
</pre>

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.