severndigital Posted March 13, 2009 Share Posted March 13, 2009 I am trying to parse information in an html table here is the table <TABLE BORDER=1 cellspacing=0 cellpadding=3> <TR><TD><B>Name</B></TD><TD>NAME</TD></TR><TR><TD><B>Address</B></TD><TD>Address 1<br /> Address 2<br /> City State Zip<br /> </TD></TR> <TR><TD><B>Phone</B></TD><TD>PHONE</TD></TR> <TR><TD><B>Fax</B></TD><TD>FAX</TD></TR> <TR><TD><B>URL</B></TD><TD>URL</TD></TR> <TR><TD><B>E-mail</B></TD><TD>EMAIL</TD></TR> </TABLE> Ideally i would like to get the info into an array that looks like the table array('Name'=>'NAME',Address'=>'ADDRESS') and so on. but would just settle for an array of any kind. I can do simple string matches, but nothing this complex, could someone please help me out. Thanks in advance.. C Quote Link to comment https://forums.phpfreaks.com/topic/149310-new-to-regex-first-attempt/ Share on other sites More sharing options...
.josh Posted March 13, 2009 Share Posted March 13, 2009 Sorry dude, but there's nothing there to uniquely identify those things. If there will be no other tables/td tags before that stuff, and the first 4 td tags will always have the stuff you want, you could regex that, by just matching stuff between td tags in general, and just taking the first 4 matches: preg_match_all('~<td>.*?</td>~is',$content,$matches); $info = array($matches[0][0] => $matches[0][1], $matches[0][2] => $matches[0][3]); Quote Link to comment https://forums.phpfreaks.com/topic/149310-new-to-regex-first-attempt/#findComment-784123 Share on other sites More sharing options...
severndigital Posted March 16, 2009 Author Share Posted March 16, 2009 OK fair enough .. is there anyway i can extract just that whole table then?? for example it is the only table on the page with this as the tag. <TABLE BORDER=1 cellspacing=0 cellpadding=3> once i have isolated that table, i could do a substr or something to get the information out that i need. Thanks again for all the help the tip of the regex iceburg is at least starting to make sense. Quote Link to comment https://forums.phpfreaks.com/topic/149310-new-to-regex-first-attempt/#findComment-785761 Share on other sites More sharing options...
.josh Posted March 16, 2009 Share Posted March 16, 2009 preg_match('~<TABLE BORDER=1 cellspacing=0 cellpadding=3>(.*?)</TABLE>~s',$content,$matches); Quote Link to comment https://forums.phpfreaks.com/topic/149310-new-to-regex-first-attempt/#findComment-785842 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.