Reputation: 724
In VARCHAR and TEXT MySQL database datatypes, I keep seeing  characters appear before every single space after the content is inserted or updated from HTML TEXTAREA fields.
When I vardump the PHP data prior to an insert/update, the  characters aren't there.
I tried converting the database and tables from the default collation of latin1_swedish_ci to utf8_general_ci encoding, then inserting/updating the data again, but the  characters still appeared in the text before each space.
I honestly don't have a very good grasp on collation and character encoding and thought things would be fine when left as default, but then I encountered this issue. How can I prevent these extra characters from appearing?
[edit]: If I update text to the database the first time, the extra characters do not appear. If I load the text from the database field and then update it a second time, then the  characters appear.
Upvotes: 1
Views: 2265
Reputation: 724
It turns out that there was problem with the PHP library I was using to generate the input/textarea fields. For the textarea fields, it was replacing all the spaces in the value with nbsp special characters. So, things went a bit wonky when storing and re-storing that value to the database. I disabled that feature.
Thank you everyone for taking the time to offer your suggestions.
Upvotes: 1
Reputation: 350
I think you might have some hidden non-ascii in the text/html. Perhaps you should try using
$text= str_replace('`', "'", $text);
$text= preg_replace("/[^(\x20-\x7F)]*/",' ', $text);
to strip non-ascii characters before you insert it into the database.
Upvotes: 1
Reputation: 473
Try to execute SET NAMES 'utf8'
on database init. Also check the encoding of your PHP files.
Upvotes: 1