php utf-8 encoding for chinese text - php

I am doing migration to generate SQL from one DB to another.
I am trying to get the output
But when I did a mb_convert_encoding("Mr.Wang (王老板)", 'UTF-8', 'Windows-1252')
I have the output as
I have those two extra "box". Any idea what am I doing wrong?
phpMyAdmin is able to export my old database containing chinese text in correct format, how do it do that in script?
*updated the images to better show my view

Have you tried setting the header in the script to UTF8? What I normally use is the following:
header('Content-Type: text/html; charset=utf-8');
That has worked for me so far for German characters & some Arabic & Japanese etc.

I found that I actually need to
mysql_query("SET NAMES 'utf8'");
before my select statement. And I do not need to run mb_convert_encoding("Mr.Wang (王老板)", 'UTF-8', 'Windows-1252') at all.
Now if I write my insert sql I got the correct text i wanted.

Related

Insert Russian characters mysql

I'm using mysql database and php to insert russian characters into a table.
I'm using:
$conn->set_charset('utf-8');
into my .php page to set charset to utf-8 but, when I try to print the DB charset with:
echo "set name:".$conn->character_set_name();
it shows
set name:latin1
I've set my Table to:
utf8mb4_unicode_ci
but nothing change.
Printing the passed text from the ajax request, I can see the text written correctly.
What should I do?
I guess you aren't checking the return value of mysqli::set_charset(). It must be returning false because utf-8 is not a valid encoding name in MySQL; the correct name is utf8 (no dash). Or, even better, utf8mb4.
You can get a list of supported encodings with:
SHOW COLLATION;

Sentrifugo - PHP can't display Unicode (Vietnamese) correctly

I'm using Sentrifugo open source HRM software. When I insert Unicode text data, the page displays it properly, except for some tables like this.
For example, this website shows it incorrecty: Nguyá»�n Thá»� Hà Linh, but when I point at that to show the title or click View detail, the text displays it correctly: Nguyễn Thị Hà Linh.
The database also changes to utf8.
I appended two lines above in to application.ini.
I also use SET NAME UTF 8:
$mysqlPDO = new PDO('mysql:host='.SENTRIFUGO_HOST.';dbname='.SENTRIFUGO_DBNAME.'',SENTRIFUGO_USERNAME, SENTRIFUGO_PASSWORD,array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$mysqlPDO->exec("set names utf8");
All columns in every table are utf8.
Please see these images:
- Database
Column
Somewhere on web show well
Somewhere show symbol (like ASCII)

mysql & UTF8 Issue with arabic

this might look like a similar issues for utf8 and Arabic language with MySQL database but i searched for result and found none..
my database endocing is set to utf8_general_ci ,
i had my php paging to be encoded as ansi by default
the arabic language in database shows as : ãÌÑÈ
but i changed it to utf8 ,
if i add new input to database , the arabic language in database shows as : زين
i dont care how it show indatabase as long as it shows normally in php page ,
after changing the php page to utf8 , when adding input than retriving it , if show result as it should .
but the old data which was added before converting the page encoding to uft8 show like this : �����
i tried a lot of methods for fixis this like using iconv in ssh and php , utf8_decode() utf8_encode() .. and more but none worked .
so i was hoping that you have a solution for me here ?
update :: Main goal was solved by retrieving data from php page in old encoding ' windows-1256' than update it from ssh .
but one issue left ::
i have some text that was inserted as 'windows-1256' and other that was inserted as 'utf-8' so now the windows encoding was converted to utf-8 and works fine , but the original utf-8 was converted as well to something unreadable , using iconv in php, with old page encoding ..
so is there a way to check what encoding is original in order to convert or not ?
Try run query set name utf8 after create a DB connection, before run any other query.
Such as :
$dbh = new PDO('mysql:dbname='.DB_NAME.';host='.DB_HOST, DB_USER, DB_PASSWORD);
$dbh->exec('set names utf8');

set charset when saving files with php

I created a simple code for uploading pictures to a folder, with PHP.
On the server side I have
<?php
header('Content-Type: text/plain; charset=utf-8');
//check if file is actually an image etc.
//if is an image, send it to "upload" folder
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
//save to the database a string like "upload/myImage.jpg", so I can render it on the site later
$stu = $dbh->prepare("UPDATE multi SET m_place=:name WHERE m_id = :id");
$stu->bindParam(':name', $n, PDO::PARAM_STR);
$n= "upload/".$_FILES["file"]["name"];
$stu->execute();
The problem?
If the name of the image is in english in the folder I see a "myImage01.jpg" and in the database also "upload/myImage01.jpg". But, if the name of the image is in greek in the folder I see "χωΟΞ―Ο‚ τίτλο.jpg" and in the db "upload/χωΟΞ―Ο‚ τίτλο.jpg".Which is wrong. Insted of χωΟΞ―Ο‚ τίτλο I should get "χωρις τιτλο" (thats greek for "no title" btw). So , I guess charset problem?
How do I fix this?
Thanks in advance
It sounds like your database doesn't have the correct collation. Make sure the tables/columns are using utf8_general_ci for their collation.
Also extremely important when handling UTF8 is to use the following two MySQL lines for GET requests...
SET time_zone = '+00:00'
SET CHARACTER SET 'utf8'
...and when you have a POST request use the following two...
SET time_zone = '+00:00'
SET NAMES 'utf8'
These will help ensure that UTF8 characters are maintained correctly.
Finally, I figure out that "PHP filesystem functions can only handle characters that are in system codepage". Thanks to this I solve my problem.
I used the iconv function
So I changed the move_uploaded_file line like so
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . iconv('UTF-8', 'Windows1253',$_FILES["file"]["name"]));

Cakephp Finnish language not coming out properly

In Cake, I have this issue with Finnish language not displaying properly. I have set utf encoding in config.php, charset output in default.ctp and also config in core.php
Is there a reason why it's not coming out properly?
To give you an idea the link is below:
http://www.likeslomakkeet.net/petitions/add
What if you re-import your data to database after changed your database.php and database collations? Try re-adding any commune with special characters like "Hämeenkyrö" and see how it looks like in database.
edit: You could also filter out all communes with "(lakkautettu)" because they no longer exists.
Did you also set the database connection to UTF-8 in database.php?
For MySQL, that would be:
'encoding' => 'utf8' // no hyphen

Categories