-1

I want to import CSV file data into MySql using PHP.I got the code to do this. The data in CSV may contain single quotes and commas within it.I used str_replace to fix the issue.It replaces only one of the characters.But I want to replace both.

example:

Broad Oak Road, Accrington, Lancashire, BB5 2AW United Kingdom.

Road King's College Aberdeen

I have used str_replace("'","\'",$csv_array[0]) to replace single quotes.Is there any way to replace both using a single PHP function? Please help me.Thanks in advance.

Nithinkumar CN
  • 287
  • 1
  • 5
  • 18
  • Why would you *replace* those characters? What "issue" is there? – deceze Oct 24 '14 at 07:25
  • The issue is when I try to insert the CSV data to my DB,it shows syntax error.That's why I tried to replace it. – Nithinkumar CN Oct 24 '14 at 07:27
  • 1
    Then you need to properly prepare your values to be entered into the database. Removing characters is not the answer! Read [The Great Escapism (Or: What You Need To Know To Work With Text Within Text)](http://kunststube.net/escapism/). – deceze Oct 24 '14 at 07:28
  • What is your field seperator? Anything aside a comma? Instead you can't differenciate between sentence marks and field separators. Or do you want to store the whole stinge in one db field? – boulder_02 Oct 24 '14 at 07:33
  • when you replace the commas and quotes from a text, you may change its meaning. Thats to be aware of! Better to escape the crucial characters – boulder_02 Oct 24 '14 at 07:35

1 Answers1

0

You can try:

str_replace('"', "", $string);
str_replace("'", "", $string);

I know it's not one line of code, but this will work also.

Refilon
  • 3,334
  • 1
  • 27
  • 51