Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UTF-8 characters don't display correctly

This is my PHP code:

<?php
$result = '';
$str = 'Тугайный соловей';
for ($y=0; $y < strlen($str); $y++) {
    $tmp = mb_substr($str, $y, 1);
    $result = $result . $tmp;
}
echo 'result = ' . $result;

The output is:

Тугайный Ñоловей

What can I do? I have to put $result into a MySQL database.

like image 306
Theo Smeets Avatar asked Apr 20 '11 16:04

Theo Smeets


People also ask

What is an invalid UTF-8 character?

Non-UTF-8 characters are characters that are not supported by UTF-8 encoding and, they may include symbols or characters from foreign unsupported languages. We'll get an error if we attempt to store these characters to a variable or run a file that contains them.

Can UTF-8 represent all characters?

Each UTF can represent any Unicode character that you need to represent. UTF-8 is based on 8-bit code units. Each character is encoded as 1 to 4 bytes. The first 128 Unicode code points are encoded as 1 byte in UTF-8.

Is UTF-8 character set or encoding?

UTF-8 is a character encoding system. It lets you represent characters as ASCII text, while still allowing for international characters, such as Chinese characters. As of the mid 2020s, UTF-8 is one of the most popular encoding systems.


1 Answers

Just add this line at the beginning, after the connection with server:

mysqli_set_charset($conn,"utf8");
like image 108
Behnam Chaghajerdi Avatar answered Sep 19 '22 22:09

Behnam Chaghajerdi