jjk-duffy Posted April 17, 2010 Share Posted April 17, 2010 I am importing a CSV file into a mysql db. I want to replace a character ( ’ ) that sometimes appears in the description column ($col3) with ( ' ). here is my code: if($import_file){ $handle = fopen($admin_folder.'csv\\'.$file, "r"); $count = 0; while(($data = fgetcsv($handle, 1000, ",")) !== false){ $count = $count + 1; if($count != 1){ list($col1, $col2, $col3, $col4) = $data; $col1 = mysql_real_escape_string($col1); $col2 = mysql_real_escape_string($col2); $col3 = mysql_real_escape_string($col3); $col3 = str_replace("’","'",$col3); $col4 = mysql_real_escape_string($col4); if($col1 != ''){ $sql="INSERT INTO auction_items SET lot='$col1', item='$col2', description='$col3', notes='$col4', auction_id='$aid'"; mysql_query($sql); } } } fclose($handle); } //end upload of csv data into table I am new to the PHP world and just can't seem to figure out why it won't work. I also tried replacing the offending character when I output the data, but it does not work there either. Quote Link to comment https://forums.phpfreaks.com/topic/198817-str_replace-help/ Share on other sites More sharing options...
jjk-duffy Posted April 17, 2010 Author Share Posted April 17, 2010 Forgot to provide an example of the offending text: Altec AT200-AV, 35’ Telescopic Non-Insulated Single-Man Bucket, s/n _____________, mtd behind cab on 2000 Ford E350 Cargo Van, 8 cyl, Auto, A/C (Exempt From Odometer Disclosure) Quote Link to comment https://forums.phpfreaks.com/topic/198817-str_replace-help/#findComment-1043521 Share on other sites More sharing options...
-Karl- Posted April 17, 2010 Share Posted April 17, 2010 $replace = array("’" => '""); $col3 = str_replace($replace,$col3); Try that. Quote Link to comment https://forums.phpfreaks.com/topic/198817-str_replace-help/#findComment-1043522 Share on other sites More sharing options...
jjk-duffy Posted April 17, 2010 Author Share Posted April 17, 2010 $replace = array("’" => '""); $col3 = str_replace($replace,$col3); Try that. When I add that code, nothing is imported into the description field. I did change make one change as you had the single and double quotes switched. $replace = array("’" => "'"); Quote Link to comment https://forums.phpfreaks.com/topic/198817-str_replace-help/#findComment-1043526 Share on other sites More sharing options...
ChemicalBliss Posted April 17, 2010 Share Posted April 17, 2010 You need to sanitize the string for mysql -after- you replace the offending characters, you can do both on one line: $col3 = mysql_real_escape_string(str_replace("’","'",$col3)); -cb- Quote Link to comment https://forums.phpfreaks.com/topic/198817-str_replace-help/#findComment-1043555 Share on other sites More sharing options...
jjk-duffy Posted April 17, 2010 Author Share Posted April 17, 2010 You need to sanitize the string for mysql -after- you replace the offending characters, you can do both on one line: $col3 = mysql_real_escape_string(str_replace("’","'",$col3)); -cb- Updated the code as suggested and this does not catch the offending character. What is really getting me is that the following will work: $s = "Altec AT200-AV, 35’ Telescopic Non-Insulated Single-Man Bucket, s/n _____________, mtd behind cab on 1996 Ford E350 Cargo Van, 8 cyl, Auto, A/C (Exempt From Odometer Disclosure)"; $s = str_replace("’","'", $s); echo ('new string is: '.$s.'<br/>'); but if I get the text via the file upload or from query of the db, it will not replace the text. Quote Link to comment https://forums.phpfreaks.com/topic/198817-str_replace-help/#findComment-1043633 Share on other sites More sharing options...
oni-kun Posted April 17, 2010 Share Posted April 17, 2010 but if I get the text via the file upload or from query of the db, it will not replace the text. The quote is most likely encoded as an HTML entity and is not caught by the str_replace call. Use var_dump on the output of the file or query and make sure the quote is indeed what you think it is, it is always useful to debug and check inputs before assuming a function does not work. Quote Link to comment https://forums.phpfreaks.com/topic/198817-str_replace-help/#findComment-1043638 Share on other sites More sharing options...
jjk-duffy Posted April 17, 2010 Author Share Posted April 17, 2010 but if I get the text via the file upload or from query of the db, it will not replace the text. The quote is most likely encoded as an HTML entity and is not caught by the str_replace call. Use var_dump on the output of the file or query and make sure the quote is indeed what you think it is, it is always useful to debug and check inputs before assuming a function does not work. OK, ran var_dump and it yields: string(177) "Altec AT200-AV, 35� Telescopic Non-Insulated Single-Man Bucket, s/n _____________, mtd behind cab on 2000 Ford E350 Cargo Van, 8 cyl, Auto, A/C (Exempt From Odometer Disclosure)" The output is consistent with what is being displayed on the website and the " � " is what I am trying to get rid of. Is this what you were wanting me to determine by running var_dump? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/198817-str_replace-help/#findComment-1043644 Share on other sites More sharing options...
oni-kun Posted April 17, 2010 Share Posted April 17, 2010 I meant on the input, such as you queries. Notice you are using mysql_real_escape_string prior to replacing, This will not fix the problem with your single quote in col3 and is most likely breaking the SQL statement. Quote Link to comment https://forums.phpfreaks.com/topic/198817-str_replace-help/#findComment-1043647 Share on other sites More sharing options...
jjk-duffy Posted April 17, 2010 Author Share Posted April 17, 2010 I meant on the input, such as you queries. Notice you are using mysql_real_escape_string prior to replacing, This will not fix the problem with your single quote in col3 and is most likely breaking the SQL statement. Here is the current version of the code based on the prior input. I put the var_dump inside the while loop so I could verify what was being placed into the variable. I also added a $stop variable that further on stops the execution of additional code and allows me to see what is being outputted. I believe that I am executing the mysql_real_escape_string and str_replace in the proper order. I have only been working with PHP for a couple of months and I really do appreciate your help. Sorry if I am not completely grasping what you are telling me, it might take me a bit for the "light to come on" Do find any other issues with the code that are preventing the desired outcome. i f($import_file){ $handle = fopen($admin_folder.'csv\\'.$file, "r"); $count = 0; $stop = 'ABC'; while(($data = fgetcsv($handle, 1000, ",")) !== false){ $count = $count + 1; if($count != 1){ list($col1, $col2, $col3, $col4) = $data; var_dump($col3); $col1 = mysql_real_escape_string($col1); $col2 = mysql_real_escape_string($col2); //$col3 = mysql_real_escape_string(str_replace("’","'",$col3)); $find = array("’"); $replace = array("'"); $col3 = str_replace($find,$replace,$col3); $col3 = mysql_real_escape_string($col3); $col4 = mysql_real_escape_string($col4); if($col1 != ''){ $sql="INSERT INTO auction_items SET lot='$col1', item='$col2', description='$col3', notes='$col4', auction_id='$aid'"; mysql_query($sql); } } } fclose($handle); } //end upload of csv data into table Quote Link to comment https://forums.phpfreaks.com/topic/198817-str_replace-help/#findComment-1043656 Share on other sites More sharing options...
ChemicalBliss Posted April 17, 2010 Share Posted April 17, 2010 Those 4 lines do exactly he same as my one line, jsut to let you know. It may be to do with encoding, why do you want this removed? is it special characters you want removed? Perhaps there is a better way of doing what you want to do. -cb- Quote Link to comment https://forums.phpfreaks.com/topic/198817-str_replace-help/#findComment-1043814 Share on other sites More sharing options...
jjk-duffy Posted April 17, 2010 Author Share Posted April 17, 2010 Those 4 lines do exactly he same as my one line, jsut to let you know. It may be to do with encoding, why do you want this removed? is it special characters you want removed? Perhaps there is a better way of doing what you want to do. -cb- I am open to an alternative method of removing the offending character. We want this removed as it is important that we have as accurate of a description as possible for the units that are included in the auction and the owner hates seeing the � on the website. We attempt to purge the character prior to loading the CSV file but alas some are more diligent than others in that pursuit. My current alternative method is to edit the database by hand. Quote Link to comment https://forums.phpfreaks.com/topic/198817-str_replace-help/#findComment-1043869 Share on other sites More sharing options...
ChemicalBliss Posted April 17, 2010 Share Posted April 17, 2010 that character appears because of the encoding you have used on your page. Have a google for utf8 character encoding, on the description insrt script, make sure you encode the string with htmlentities. that should fix it and you wont have to replace anything. so, where/what script isinserting thi description? -cb- Quote Link to comment https://forums.phpfreaks.com/topic/198817-str_replace-help/#findComment-1043880 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.