-1

My script is writing to a text file so it can be cleaned of junk before being uploaded to a Mysql database. I need to selectively remove all characters that would cause a Mysql upload issue while preserving my format for uploading to the database

('column1' , 'column2' , 'column3'),

Above is the structure I need to maintain. So if my data has junk it it, I need it to convert to something that the database can read without it thinking it is code and throw an error at me and stopping the upload. So if raw data looks like

('$%^co--><``lumn1' , 'col[um]n2' , '##colFTumn3'),

I need it to look like

('column1' , 'column2' , 'column3'),

Any ideas on how to write a php code that will open file, clean it, and then put the contents back in the original file?

Basically I need it to remove everything that is not a number, letter or any character that is not found in a URL which would include the // and ? etc

  • If you can specify what characters you would like removed then we can help you. – Ali Sep 04 '13 at 15:52
  • possible duplicate of [RegEx for Javascript to allow only alphanumeric](http://stackoverflow.com/questions/388996/regex-for-javascript-to-allow-only-alphanumeric) @user2736232 Have you even tried to find a solution on your own? "please give full php code" does not belong to the scope of this website. Maybe you should hire someone to do your work. – feeela Sep 04 '13 at 15:56
  • **Try writing something yourself** and then if it doesn't work, bring it to us to help you along. You start it, we help. We don't write it for you. Show us the actual code that you've tried and then we can help you from there. Chances are you'll get pretty close to the answer if you just try it yourself first. – Andy Lester Sep 04 '13 at 16:01

2 Answers2

2

use code like,

$string = str_replace("-","",preg_replace('/[^A-Za-z0-9\-]/', '', "$%^co--><``lumn1"));

and so on.

Yatin Trivedi
  • 661
  • 6
  • 9
0

Since the aim is to avoid the DB thinking it is SQL then removing characters isn't the best way to go as you're going to lose data.

I'd recommend you use mysqli::real_escape_string() instead.

pedro_sland
  • 4,765
  • 1
  • 16
  • 16