blommer Posted November 8, 2009 Share Posted November 8, 2009 Hello everyone, hopefully someone can help me out. I have a page for my school's lunch inventory, and I want to capture just the Vegetable type and number (the HTML I posted below is just an example). Could anyone give me some tips on how to start? Eventually I would like to enter this information in a database, but for right now I can make do with just inserting it into an HTML table like this: Corn Cobs 3 Lettuce Heads 10 Potatoes 3 <HTML> <HEAD> <TITLE>Inventory</TITLE> </HEAD> <BODY> <H2>Inventory</H2> for <B>Monday, December 5, 2009</B><BR> <BR> <A NAME="I1"> <B>Fruits</B><BR> <FONT SIZE="-1"><A NAME="F1"> <B>Apples</B><BR> 10<BR> <B>Pears</B><BR> 5<BR> </FONT> <FONT SIZE="-2"><A HREF="index.html">Return to home...</A></FONT><BR CLEAR="LEFT"> <HR> <B>Vegetables</B> <BR> <FONT SIZE="-1"><A NAME="V1"> <B>Corn Cobs</B><BR> 3<BR> <A NAME="S5795_3"><B>Lettuce Heads</B><BR> 10<BR> <A NAME="S5795_5"><B>Potatoes</B><BR> 3<BR> </FONT> <FONT SIZE="-2"><A HREF="index.html">Return to home...</A></FONT><BR CLEAR="LEFT"> <HR> <BR> </BODY> </HTML> Quote Link to comment Share on other sites More sharing options...
thebadbad Posted November 8, 2009 Share Posted November 8, 2009 If we first split the string at the Vegetables header, we can then grab each vegetable and amount with a regular expression and then do whatever we want with the data: <?php $html = <<<HTML <HTML> <HEAD> <TITLE>Inventory</TITLE> </HEAD> <BODY> <H2>Inventory</H2> for <B>Monday, December 5, 2009</B><BR> <BR> <A NAME="I1"> <B>Fruits</B><BR> <FONT SIZE="-1"><A NAME="F1"> <B>Apples</B><BR> 10<BR> <B>Pears</B><BR> 5<BR> </FONT> <FONT SIZE="-2"><A HREF="index.html">Return to home...</A></FONT><BR CLEAR="LEFT"> <HR> <B>Vegetables</B> <BR> <FONT SIZE="-1"><A NAME="V1"> <B>Corn Cobs</B><BR> 3<BR> <A NAME="S5795_3"><B>Lettuce Heads</B><BR> 10<BR> <A NAME="S5795_5"><B>Potatoes</B><BR> 3<BR> </FONT> <FONT SIZE="-2"><A HREF="index.html">Return to home...</A></FONT><BR CLEAR="LEFT"> <HR> <BR> </BODY> </HTML> HTML; list(, $html) = explode('<B>Vegetables</B> <BR>', $html, 2); preg_match_all('~<B>([^<]+)</B><BR>\s*([0-9]+)<BR>~i', $html, $matches, PREG_SET_ORDER); //print structured data echo '<table>'; foreach ($matches as $match) { echo "\n\t<tr><td>{$match[1]}</td><td>{$match[2]}</td></tr>"; } echo "\n</table>"; ?> Quote Link to comment Share on other sites More sharing options...
blommer Posted November 8, 2009 Author Share Posted November 8, 2009 Thanks thebadbad, this is a good start for me! Quote Link to comment 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.