PHP: How to convert ISO character (HTMLEntities) to UTF-8?


convert htmlentities to utf8, convert iso to utf8, convert string to utf-8, convert string to utf8, php character encoding problem, php convert iso to utf8, php how toI’m facing problem in how to convert string from ISO to UTF8 previously. Due to the server configuration problem, all the UTF-8 char has been convereted into ISO (HTMLEntities) before it insert into db and those ISO character (HTMLEntities) break while showing in XML document. Now i found the solution to convert ISO character into UTF-8.

The original utf-8 chinese words: “你好”.
Converted to ISO (HTMLEntities) : “你好”

For you to convert the ISO string “你好” to become utf-8 chinese words: “你好”, you need to use Multibyte String function (or mbstring extension). Example below uses mb_convert_encoding function to convert ISO (HTMLEntities) characters to UTF-8:-

Advertisements

<?php
$str = “&#20320;&#22909;”;
echo mb_convert_encoding($str, ‘UTF-8’, ‘HTML-ENTITIES’);
?>

Output will be 你好.

*** To find out if the conversion from ISO to utf-8 is working, you need to view source (view the html source code) to check if the character is in UTF-8 format. (do not trust what your browser shows you!) ***

This example need u to have mbstring extension installed. By default, mbstring extension is not installed. If you do not have mbstring installed you wont be able to convert the ISO string to UTF-8 using the method i show here. You may need to contact your server administrator to install mbstring extension.

[tags]php character encoding problem, convert iso to utf8, convert htmlentities to utf8, convert string to utf-8, convert string to utf8, php convert iso to utf8, php how to[/tags]




Share this with your friends:-

9 Responses to “PHP: How to convert ISO character (HTMLEntities) to UTF-8?”

  1. Ult_Combo says:

    Very useful tip!

    I had a similar problem, the only difference is that I needed the completely opposite. I needed to convert UTF-8 to HTML-ENTITIES to be able to properly display it in an ISO-encoded page.

    I don’t have much experience with Multibyte string functions, and the mb_convert_encoding demonstrated by you did it just fine! I just had to change the parameters order to convert from UTF-8 to ISO:

    $str = mb_convert_encoding($str, ‘HTML-ENTITIES’, ‘UTF-8’);

    Thanks again.

  2. stylus says:

    murali says:
    May 7, 2010 at 2:50 pm
    thanks for the useful tip.But i need to write back the converted data to mysql table.but it fails to do that.please help in this issue,here i m posting related data
    Before Conversion:أعرف ان ك
    HTML Outout:أعرف ان ك
    after conversion:نعم، هذا صحيح.
    i want save the exact html out put to mysql table
    plz helpppppppppppp

    ANS:
    To save data into Mysql you need to use

    mysql_query(“set names utf8”):
    It is a connection collation for Mysql.

  3. murali says:

    this is the actual html entity string
    &#1571&#1593&#1585&#1601 &#1575&#1606&#1603

    put semicolon(;)before & symbol(except first one)so that it can be a valid html entity string.

  4. murali says:

    sorry before converted string is in html entities as ‘أعرف ان &#1603’

  5. murali says:

    thanks for the useful tip.But i need to write back the converted data to mysql table.but it fails to do that.please help in this issue,here i m posting related data

    Before Conversion:أعرف ان ك
    HTML Outout:أعرف ان ك
    after conversion:نعم، هذا صحيح.
    i want save the exact html out put to mysql table
    plz helpppppppppppp

  6. […] PHP: How to convert ISO character (HTMLEntities) to UTF-8? […]

  7. pizzaarapicana says:

    if we have a rss feed that is encoded in widows-1250 and our web site is encoded in UTF-8 ..how we can then make visible on the web site the data that we are retrieving from that feed ?

    I dont want to change web site’s enoding..

    help ?plz..

  8. SoGua says:

    if you have just few records then copy and paste is not the big problem, but if you want to convert a large number of record then you need to use this method to do a batch conversion

  9. hanneng says:

    thanks for sharing the tips.
    previously, I also facing the same problem,
    the end, copy and paste …….

Leave a Reply