Using str_split for a UTF-8 encoded string

I am currently working on a project, and instead of the usual MySQL queries, I decided to go ahead and learn how to use PDO.

I have a table with the names of the contestants, the database, the table and all the columns are in utf-8. I have ten entries in the members table, and their name column contains characters such as ääö.

Now, when I retrieve a record from the database and change the name var_dump, I get a good result - a string with all the special characters intact. But I need to split the string by characters to get them in an array, which I then shuffle.

For example, I have this line: Test ÅÄÖ Tåän

And when I run str_split, I get each character in its own key in the array. The only problem is that all special characters are displayed as follows: which means that the array will look like this:

Array ( [0] => T [1] => e [2] => s [3] => t [4] => [5] =>   [6] =>   [7] =>   [8] =>   [9] =>   [10] =>   [11] => [12] => T [13] =>   [14] =>   [15] =>   [16] =>   [17] => n ) 

As you can see, it not only spoils the characters, but also duplicates them in the str_split process. I tried several ways to break the string, but they all have the same problem. When I print a line before splitting, it displays special characters well.

This is my dbConn.php code:

// Requires a configuration file: require_once ('config.inc.php');

 // Start PDO connection: $dbHandle = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=utf-8", $dbUser, $dbPass); $dbHandle -> exec("SET CHARACTER SET utf8"); // Set error reporting: $dbHandle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 

And this is the code that I use to retrieve from the database and the loop:

 // Require files: require_once('dbConn.php'); // Get random artist: $artist = $dbHandle->query("SELECT * FROM ".ARTIST_TABLE." WHERE id = 11 ORDER BY RAND() LIMIT 1"); $artist->setFetchMode(PDO::FETCH_OBJ); $artist = $artist->fetch(); var_dump($artist->name); // Split name: $artistChars = str_split($artist->name); 

I am connecting to utf-8, my php file is utf-8 without specification, and no other special characters on this page share this problem. What could be wrong or what am I doing wrong?

+45
string php mysql pdo utf-8
Oct. 19 '11 at 13:58
source share
7 answers

str_split does not work with multibyte characters, it returns only the first byte - thus invalidating your characters. you can use mb_split .

+12
Oct 19 2018-11-11T00:
source share

Remember that the utf8 declaration used in your connection string is reportedly not working. In the comments on php.net, I often see this alternative:

 $dbHandle = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=utf8", $dbUser, $dbPass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'")); 
+150
Jun 28 '12 at 17:14
source share

UTF-8 using PDO

problems writing international (even Chinese and Thai) characters to the database

there may be more ways to do this job. I am not an expert, just a technical specialist, interested in understanding all this. On Linux and Windows, I installed several CMS (content management systems) using the example from the following website:

" http://www.elated.com/articles/cms-in-an-affter-php-mysql "

The sample uses PDO to insert, update, and delete.

It took me a few hours to find a solution. Whatever I do, I always made a distinction between the data in my forms and in phpmyadmin / heidi -views

I followed the prompts: " https://mathiasbynens.be/notes/mysql-utf8mb4 ", but there was still no success

There is a “Config.php” file in my CMS structure: after reading this web page I changed the line

  define( 'DB_DSN', 'mysql:host=localhost;dbname=mythings); 

at

  define( 'DB_DSN', 'mysql:host=localhost;dbname=mythings;charset=utf8'); 

Now everything is working fine.

+10
Jan 22 '17 at 4:49 on
source share

The str_split function str_split separated by a byte, not a character. You will need mb_split .

+4
Oct 19 2018-11-11T00:
source share

this work for me ... hope this is helpful.

make sure the database, apache and each config were in utf8.

OBJECT PDO

  $dsn = 'mysql:host=' . Config::read('db.host') . ';dbname=' . config::read('db.basename') .';charset=utf8'. ';port=' . Config::read('db.port') .';connect_timeout=15'; $user = Config::read('db.user'); $password = Config::read('db.password'); $this->dbh = new PDO($dsn, $user, $password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'")); $this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 

it works if no other function is used, such as str_word_count.

USING str_word_count you need to use utf8_decode (utf8_encode) ..

 function cortar($str) { if (20>$count=str_word_count($str)) { return $str; } else { $array = str_word_count($str,1,'.,-0123456789()+=?¿!"<>*ñÑáéíóúÁÉÍÓÚ@|/%$#¡'); $s=''; $c=0; foreach ($array as $e) { if (20>$c) { if (19>$c) { $s.=$e.' '; } else { $s.=$e; } } $c+=1; } return utf8_decode(utf8_encode($s)); } } 
Function

returns a string with 20 words.

+3
Jul 30 '15 at 6:28
source share

UTF-8 PROBLEMS AND SOLUTIONS WITH PHP FUNCTIONS

1. How to save charterers UTF-8 (mathematical string, special characters like 92 ÷ 8 ÷ 2 =?)?

Repl. $ string = utf8_encode ('92 ÷ 8 ÷ 2 =? ');

2. How to print UTF-8 charterers from a database?

Repl. echo utf8_decode ($ string);

Note. If you do not want to do this with encoding / decoding, you can do it with.

1. if you are using mysqli_query (), then

 $conn = mysqli_connect('localhost','db_username','password','your_database_name'); mysqli_set_charset($conn,"utf8"); 

2. If you use PDO, then

 class Database extends PDO{ function __construct() { parent::__construct("mysql:host=localhost;dbname=your_db_name","gurutslz_root","Your_db_password",array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'")); } } $conn=new Database(); 
+1
Jan 17 '17 at 8:31 on
source share

I only had problems with text fields in my database structure storing product descriptions. I set the field settings for blob instead of text, which solved my problem.

0
May 01 '19 at 22:07
source share



All Articles