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

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 ).
Who is Participating?
hernst42Connect With a Mentor Commented:
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);
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.
lewisbdAuthor Commented:
Some of the files are encoded with iso-8859-1 but some are in windows-1252, and extension of iso-8859-1
lewisbdAuthor Commented:
Since not all of the files are in the same format, the iconv function creates an empty file where the format is not CP1252.
lewisbdAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.