Using str_split on a UTF-8 encoded string
str_split
does not work with multi-byte characters, it will only return the first byte - thus invalidating your characters. you could use mb_split
.
Mind that the utf8 declaration used in your connect-string is reported to be not working. In the comments on php.net I frequently see this alternative:
$dbHandle = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=utf8", $dbUser, $dbPass,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
The str_split
function splits by byte, not by character. You'll need mb_split
.
UTF-8 Using PDO
problems when writing international (even Chinese and Thailandic) characters to the database
there may be more ways to make this work. I am not an expert, just a tech-freak, interested to understand all this. In Linux and Windows I have set up a few CMS (content-managing-systems), using a sample from the following website:
'http://www.elated.com/articles/cms-in-an-afternoon-php-mysql'
The sample is using PDO for insert, update and delete.
It took me a few hours to find a solution. Whatever I did, I always concluded differences between the data in my forms and in the phpmyadmin/heidi -views
I followed the hints of: 'https://mathiasbynens.be/notes/mysql-utf8mb4' but there was still no success
In my CMS-structure there is a file 'Config.php': After reading this webpage I changed the line
define( 'DB_DSN', 'mysql:host=localhost;dbname=mythings);
to
define( 'DB_DSN', 'mysql:host=localhost;dbname=mythings;charset=utf8');
Now all works fine.