简体   繁体   中英

How to convert HTML code of Unicode to Real Unicode Character

Is there any php function that can convert

ابب

to it equallent unicode character

ت ب ا

I have googled a lot, But I think there is no any PHP built in function available for this purpose. Actually, I want to store the user submitted comments (that are in unicode characters) to mysql database. But It is stored in this format ابب in mysql database. I am also using SET NAMES 'utf8' in mysql query. The Output is in real unicode characters that is fine, but the insertion in mysql is in this format ابب that i don't want. Any Solution??

I googled It and found a very interesing solution here

I have also tried it and I think Its working

<?php
    $trans_tbl = get_html_translation_table(HTML_ENTITIES);
    foreach($trans_tbl as $k => $v)
    {
        $ttr[$v] = utf8_encode($k);
    }
    $text = '&#1575;&#1576;&#1576';
    $text = strtr($text, $ttr);
    echo $text;
?>

for mysql solution you can set the character set as

$mysqli = new mysqli($host, $user, $pass, $db);

if (!$mysqli->set_charset("utf8")) {
die("error");
}

if you

1 - set your html page's lang encoding to utf-8 which includes your forms
2 - only use your forms to enter input into your related MySQL db tables
3 - set all collations to utf8_unicode_ci in your MySQL (tables and rows collations)
4 - if you have premission you can also setyour MySQL DB collation as utf8_unicode_ci

then you won't see entities in your mySQL records also

This is my solution I use and have no trouble with my mother language which also has lots of unicode characters.

Below I introduce you my db connection php code & recommend (by using prepared statements; please check http://php.net/manual/en/mysqli.quickstart.prepared-statements.php )

//mysql bağlantısı
global $db_baglanti;
$db_baglanti = new mysqli(vt_host, vt_user, vt_password, vt_name);
if ($db_baglanti->connect_errno) 
{
    echo "MySQL bağlantısı kurulamadı: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if (!$db_baglanti->set_charset("utf8")) 
{
    printf("utf8 karakter setinin yüklenmesinde problem oluştu: %s\n", $db_baglanti->error);
} 
else 
{
    $db_baglanti->set_charset("utf8");
}

I think you should be able to use mysql_set_charset() http://php.net/manual/en/function.mysql-set-charset.php to set the mysql connection encoding so when you retrieve these from the DB and display them they should be fine.

According to php.net "This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM