Forum Moderators: coopster
I need to have those removed.
Can anyone show me what I need to add and where it should go?
Here is the portion of the file in question:
$fcontents = file ('./InvStatFile.txt');
# expects the csv file to be in the same dir as this script
for($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i]);
$arr = explode("\t", $line);
#if your data is comma separated
# instead of tab separated,
# change the '\t' above to ','
$sql = "insert into products_temp values ('".
implode("','", $arr) ."')";
mysql_query($sql);
echo $sql ."<br>\n";
if(mysql_error()) {
echo mysql_error() ."<br>\n";
}
}
ANY help would be very appreciated! I have messed with several commands, but I cannot get it to work. Thanks!
Burke
$data='abcdefgh';
$data = substr($data,1,-1);
// returns 'bcdefg'
If you just want to remove quotes indiscriminantly, use str_replace():
$data='"abcdefgh"';
$data = str_replace('"','',$data);
// returns 'abcdefgh'
If your data has valid quotes in it, this regex pattern will remove quotes only if they're found at the start or end of the string:
$data='"abc"def"gh"';
$data = preg_replace('/^\"(.*)\"$/','\${1}',$data);
// returns 'abc"def"gh'
That last regex pattern was just off the top of my head, it might need some tweaking
good luck!
for($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i]);
$arr = explode("\t", $line); [b]foreach($arr as $k=>$v){
$arr[$k]=str_replace('"','',$v);
}[/b]
$sql = "insert into products_temp values ('".
implode("','", $arr) ."')";
mysql_query($sql);
echo $sql ."<br>\n";
if(mysql_error()) {
echo mysql_error() ."<br>\n";
}
}
The foreach loop goes through the array and yanks the quotes out of each element. Then it's all tidy and ready to go into the database.
cheers,
httpwebwitch