Jump to content

Parsing out inventory numbers


blommer

Recommended Posts

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>

Link to comment
Share on other sites

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>";
?>

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.