How to insert a string like "Pyrénées, France'" in mysql database?
How can i开发者_如何学编程 insert string "Pyrénées, France'" in mysql database. How can i remove special characters form that string using php?
Just insert it and make sure that the charset of your database supports your special characters. It might be best to switch the charset of your database to UTF-8.
You can use iconv()
to convert the string to ASCII. By using the TRANSLIT
and IGNORE
flags it will try to substitute characters not supported in the target charset and strip any remaining invalid characters.
However, better configure your database to accept UTF8 strings so you can simply store the data without modifications.
Just insert it like that. If your encoding is correct, you will have no problems at all. You should not remove any 'special characters' here.
If you want to remove any special characters you can use this function:
$text = preg_replace('/[^a-zA-Z0-9_ -]/s', '', $text);
It will replace any non-special chars with empty char.
If you use a proper charset for your database or table (UTF-8, utf8_general_ci
), you won't have to remove special characters.
Alternatively you could use htmlspecialchars()
to convert possibly 'malicious' characters into HTML-Entities.
If you remove special characters, I see the problem that words could end up not making proper sense anymore.
精彩评论