How can I convert existing HTML files to UTF-8 encoding?

Posted on 2007-09-28
Last Modified: 2013-12-13
I am using an xmlhttprequest to display one html document inside the div tag of another html document.

When the target page is displayed, it replaces certain characters with question marks. I discovered that if I open the html file in Notepad and save it in UTF-8 format, the page loads properly, without the question marks. I'm looking for a way to write a script in PHP to open several hundred html documents of this type and save them as utf-8 encoded files, in the way tha Notepad stores them.

I have attempted to do this in PHP by:

1.  using header("Content-type: text/html; charset=utf-8"); at the top of my PHP file,

2. opening the file, inserting <meta http-equiv='Content-Type' content='text/html; charset=utf-8'> right after the <head> tag, and

3. using utf8_encode to save the file contents.

This does get rid of the question marks that were replacing certain %A0 (space) chars but, single quote marks still come out as question marks (e.g., don?t ).
Question by:lewisbd
    LVL 48

    Expert Comment

    utf8_encode can only recode from iso8859-1 to utf8. So are you sure the files you convert are in iso8859-1 format? Else such things might happen. Else your way to convert the files look ok.

    Author Comment

    Some of the files are encoded with iso-8859-1 but some are in windows-1252, and extension of iso-8859-1
    LVL 48

    Accepted Solution

    If the files are encode in windows-1252 you will need iconv to convert them. windows 1252 differs in some chars and that are the chars which cause the problems in utf8

    iconv("CP1252", "UTF-8", $filecontent);

    Author Comment

    Since not all of the files are in the same format, the iconv function creates an empty file where the format is not CP1252.

    Author Comment

    Actually, you are correct. I didn't assign the right value to a string. This was the solution I was looking for. Thank you very much.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
    These days socially coordinated efforts have turned into a critical requirement for enterprises. provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
    Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now