Reputation: 81
I have a restful web service, that gets input string, sanitizes it and saves it to MySQL database.
My workflow looks like this: I get string, I trim it. Then I remove any non alphanumeric characters, leaving underscore. And then I replace underscore with space. And then I save it to database.
$trimmedName = trim($name);
$replacedName = preg_replace("/[^0-9a-zA-Z_]/", "", $trimmedName);
$sanitizedName = trim(str_replace("_", " ", $replacedName));
Problem is, that this works fine 99% of time. But sometimes, I see in database that there is a square symbol. I dont have photo of that, but you get the idea. Im not sure what it is and why my preg_replace does not strip it out. I think someone tries to take advantage of this bug, so I would like to know what is wrong with my regex, why it leaves some unknown characters.
Upvotes: 0
Views: 100
Reputation: 695
This is probably as a result of the characters you are saving. The names have some accent on some letters. This means you check your character encoding and set it to UTF-8 in the code header.
PHP
header('Content-Type: text/html; charset=utf-8');
Also make sure your database table's character set is UTF-8
You can update the table MySQL
ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;
Upvotes: 1