I have a file:
200 Paddington 100 12.99 5.00 0.50 Our 11 inch bear is a great companion for those cold winter mornings. Quality construction, soft fur, and a red and white raglan tee will make him the envy of all your stuffed animals. \r\nSoft plush fur 11 inches tall.\r\nRed bow and t-shirt included. 1 1 graphics/00000001/bear.jpg graphics/00000001/bear-lg.jpg
100 Bagels and Lox 200 10.00 10.00 1.00 This is the talking fish 1 1
300 Monkey Around 300 5.00 5.00 1.00 This is a talking monkey. 1 1
400 I Hate Miva Bear 100 12.50 1.00 0.75 This is the famous I hate Miva Bear 1 1
I am trying to parse this file with:
<?
include("dbconnect.php");
$file = file("./miva/miva.data");
for($i=0; $i<count($file); $i++)
{
$contents = str_replace(",", " ", $file[$i]);
$contents = str_replace(" \t", ",", $contents);
$contents = explode(",", $contents);
$id = $contents[0];
$prod_name = $contents[1];
$price = $contents[3];
$weight = $contents[5];
$prod_sum = $contents[6];
$prod_desc = $contents[6];
if($contents[7] == 1){$tax = "N";}else{$tax = "Yes";}
$sm_image = $contents[8];
$lg_image = $contents[9];
$query = "insert into products values('$id', '$prod_name', '$id', '$price', '', '$tax', '0', 'N', '0', '1', '$weight', 'N', 'N', '$lg_image', '100', '$sm_image', '', '', '$prod_desc', 'none', 'General', 'General', '$item_sum', 'ea', '')";
$result = mysql_query($query, $mysql_link);
echo "$contents[0] | $contents[1] | $contents[2] | $contents[3] | $contents[4] | $contents[5] | $contents[6] | $contents[7] | $contents[8], $contents[9] <br>";
}
?>
I posted this the other day and thought I got it when I changed the \t to eight blank spaces.
The problem I found was that it seems that the tabs in this file are not being written as true tabs and the \t is not working either.
Any ideas on how to parse this file?
I tried changing blank spaces to commas like:
$contents = str_replace(" ", ",", $contents);
$contents = str_replace(" ", ",", $contents);
$contents = str_replace(" ", ",", $contents);
$contents = str_replace(" ", ",", $contents);
$contents = str_replace(" ", ",", $contents);
$contents = str_replace(" ", ",", $contents);
$contents = str_replace(" ", ",", $contents);
but the problem I run into here is if the product name or the description has more than two spaces then the whole things won't work obviously.
Any ideas would be great!