Page 1 of 1

Need help parsing text file

Posted: Wed Dec 11, 2002 7:25 pm
by b_rock_1
Greetings. I'm needing a little help parsing a text file. I've got it to kinda work, but I need a little more room for error. I'll include my examples below. The problem I'm running in to is this only works if the <TD> has an alignment of "middle". Is there a way I can wildcard that explode that puts the <TD>s in an array? What if somehow the <TD> has an alignment of left or right? The way I have it now, I'm screwed! Thanks for your assistance.

***** Example of Text file I'm parsing! *****

Code: Select all

<TR>
<TD align="middle">12345</TD>
<TD align="middle">Blue VW Beetle Conv.</TD>
<TD align="middle">
	$4.30</TD>
	
<TD align="middle">
	$6.00</TD>
		
<TD align="middle">
	$6.90</TD>
	
<TD align="middle">
	$12.95</TD>
	
<TD align="middle">1 EA</TD>
<TD align="middle">Yes</TD>
<TD align="middle"></TD>                                                                         
<TD align="middle"></TD>                      
</TR>
                				
<TR>
<TD align="middle">54321</TD>
<TD align="middle">Red VW Beetle Conv.</TD>
<TD align="middle">
	$4.95</TD>
	
<TD align="middle">
	$6.95</TD>
		
<TD align="middle">
	$7.95</TD>
	
<TD align="middle">
	$14.95</TD>
	
<TD align="middle">1 EA</TD>
<TD align="middle">Yes</TD>
<TD align="middle"></TD>                                                                         
<TD align="middle"></TD>                      
</TR>
***** End example of Text file I'm parsing! *****

***** Example of code I'm using! *****

Code: Select all

<?

// open textfile that needs to be parsed and place it in the variable $file
$file = fread(fopen("info.txt", "r"), filesize("info.txt"));
fclose($file);

// $table is going to redisplay the info to standard output
$table = "
<TABLE border=1 cellpadding=5 cellspacing=5>
    <TR>
        <TH>Item Number:</TH>
        <TH>Item Name:</TH>
        <TH>Item Price:</TH>
        <TH>In Stock:</TH>
   </TR>
";

// Throw each of the Table Rows in an array
$tr = explode ( "<TR>", $file);

$max = count($tr) - 1;

/* Cycle through the array of Table Rows one by one, exploding each <TD> into an array, and assigning the 4 <TD>s you need to the associated variable.
*/
for ($i = 1; $i <= $max; $i++)
&#123;
    // Is it possible to wildcard this part inside the <TD> tag???
    $td = explode ("<TD align="middle">", $tr&#1111;$i]);
    $itemNumber = trim(strip_tags($td&#1111;1]));
    $itemName = trim(strip_tags($td&#1111;2]));
    $itemPrice = substr(trim(strip_tags($td&#1111;6])), 1); // use substr to remove the dollar sign
    $inStock = trim(strip_tags($td&#1111;8]));

    // $text will contain the SQL statements you need.
    $text .= "update test set inStock = '$inStock', itemPrice = '$itemPrice' where itemNumber = '$itemNumber';\n";

    if ($i % 2 == 0)
    &#123;
        $bgcolor="#99ccff";
    &#125;
    else
    &#123;
        $bgcolor="#ffffcc";
    &#125;

    $table .= "<TR bgcolor = $bgcolor><TD>$itemNumber</TD><TD>$itemName</TD><TD>$itemPrice</TD><TD>$inStock</TD></TR>";

&#125;

$table .= "</table>";

echo $table;

// create or overwrite "info.sql" file with the info in $text
$sqlfile = fopen("info.sql", "w");
fwrite($sqlfile, $text);
fclose($sqlfile);

?>
***** End example of code I'm using! *****

***** Example of output I'm getting / needing. *****

Code: Select all

update test set inStock = 'YES', itemPrice = '12.95' where itemNumber = '12345';
update test set inStock = 'YES', itemPrice = '12.95' where itemNumber = '54321';

etc...
***** End example of output I'm getting / needing. *****[/quote]