Use DOMDocument to parse non utf-8 encoding web page in PHP

Recently I was digging around in PHP + curl + DOMDocument, there are quite lot of impressive facilities such as DOMxPath, curl post, cookies, it is very effortless to simulate any action on an website without JavaScript depend. Here is some problem & tricks I found when I handle any non utf-8 encoding with CURL & DOMDocument.

Case 1:
Parsing a non utf-8 encoding page to DomDocument, Some web page put tag in following sequence

Assuming you have just received the html content from curl_exec

Open your test.html with any text editor, you may find the your html body is gone & the header is incomplete.

To resolve this problem, you will have to put the title after the

Here is a simple trick to do

Now you should obtain the proper document content without lose anything.