• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1589
  • Last Modified:

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 ).
0
lewisbd
Asked:
lewisbd
  • 3
  • 2
1 Solution
 
hernst42Commented:
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.
0
 
lewisbdAuthor Commented:
Some of the files are encoded with iso-8859-1 but some are in windows-1252, and extension of iso-8859-1
0
 
hernst42Commented:
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);
0
 
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.
0
 
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now