unneutral Posted March 18, 2007 Share Posted March 18, 2007 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>[email protected]</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>[email protected]</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 https://forums.phpfreaks.com/topic/43216-retrieving-html-data-from-a-table/ Share on other sites More sharing options...
effigy Posted March 19, 2007 Share Posted March 19, 2007 <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>[email protected]</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 https://forums.phpfreaks.com/topic/43216-retrieving-html-data-from-a-table/#findComment-210490 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.