Forum Moderators: coopster

Message Too Old, No Replies

How to escape HTML Bullets

getcsv doesnt escape html bullets

         

phparion

12:51 pm on Sep 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi

i have a csv file for which i run a loop and use INSERT command to insert data into DB.. here is the code,

if($filehandle = @fopen('mydata.csv','r'))
{
$counter = 1;
// get headers
$headers = fgetcsv($filehandle, 1000, ",");
$fields = implode(",",$headers);
// get the data from the rest of the file line by line
$values = "";
$errors = array();
while(($line = fgetcsv($filehandle, 1000, ","))!== FALSE)
{

//print_r($line); echo "acha"; exit;
for($i = 0;$i < count($line);$i++)
{
//echo htmlspecialchars($line[$i]) ."<br>";

//filter values

$line[$i] = htmlentities($line[$i], ENT_QUOTES);
// More filtering as needed
$line[$i] = mysql_real_escape_string($line[$i]);
//$line[$i] = strip_tags($line[$i]);
}
// exit;
//echo $line[$i]; exit;
$values = "'".implode("','",$line)."'";
//do insert
$sql = "INSERT INTO product ($fields) VALUES ($values);";
//mysql_query($sql) or die('insert failure line'.$counter);
//echo $sql."<br>";
//mysql_query($sql) or die(mysql_error());

if(mysql_query($sql) === FALSE) {
print_r($line);
$errors[] = "$sql<br><br>Record # " . $counter . "has [ ".mysql_error()." ] ";
break;
}

$counter++;
//exit;
}
}
else
{
echo('error reading file');
}

foreach($errors as $err) {
echo $err . "\n\n";
}

i am having problem with HTML BULLETS

up to 700 records are INSERTed without any problem but the 701th record has HTML BULLETS in it and

PHP Code:

while(($line = fgetcsv($filehandle, 1000, ","))!== FALSE)

this step picks only first bullet value in index[0] and then breaks i.e it doesnt pick the remaining colums values in the same row... also in the next run i.e for 702th value it picks the next BULLET value from 701th row and breaks and even doesnt reach the actual 702th row..

now i am stuck that how to skip BULLETS in the values?

here is 701th record index[0] that is product description,

Code:

"<p><font face=\""Arial\""><b>Features:</b><br>
• Durable clutch mechanism for long life<br>
• Handle exhaust directs air away from the workpiece and operator<br>
• Positive action trigger for precise control<br>
• Contoured elastomer handle grip for improved comfort and ergonomics<br>
• Applications: transmissions, exhaust systems, body shops, general repair in
the 3/8\"" range<br>
• Ultimate Torque: 180 ft.-lbs.<br>
<br>
<b>Specifications:</b><br>
Square Drive (In.): 3/8<br>
Weight (Lb.): 3.5<br>
Length (In.): 6<br>
Average Air Consumption CFM: 3<br>
Air Inlet NPTF (In.): 1/4<br>
Working Torque Range ft.-lb.: 40-150<br>
Ult. Torque @ 90 PSI Fwd/Rev ft.-lb.: 180</font></p><br>No additional specifications are available at this time."

and following is the mysql errors i am getting

Code:

Record # 696has [ Column count doesn't match value count at row 1 ] INSERT INTO product (descr,graphic,image,manufacturer,model,msrp,price,quantity,sku,status,subcat,title,topcat,weight) VALUES ('• Durable clutch mechanism for long life<br>');

Record # 697has [ Column count doesn't match value count at row 1 ] INSERT INTO product (descr,graphic,image,manufacturer,model,msrp,price,quantity,sku,status,subcat,title,topcat,weight) VALUES ('• Handle exhaust directs air away from the workpiece and operator<br>');

thanks

dreamcatcher

12:40 pm on Sep 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



phparion,

Did you get this sorted out? Can you use str_replace [uk.php.net] to remove the bullets?

dc

phparion

5:02 am on Sep 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$line = fgetcsv($filehandle, 1000, ","))!== FALSE)

the problem starts here when code fetch a record from csv file.. so i dont think i can use regex to help me as it doesn't fetch a complete record but breaks after reading html bullets.. according to your suggestion i would have to apply regex to whole csv file which is 50 MB and it doesn't make sense to me.

however, i have solved the problem but with another code and file, i generated a tab delimated file instead of comma delimated and now things are working smooth with LOAD DATA INFILE...