How does php generate UTF-8 html files?

Source: Internet
Author: User
How does php generate a UTF-8 html file? How does php generate a UTF-8 html file ?? Note that not simply adding & lt; metahttp-equiv & quot; Content-Type & quot; content & quot; texthtml; charsetutf-8 & quot; & gt ;, instead, after the file is generated, use notepad to open and save the file, and the encoding is automatically UTF-8. ---- How php generates UTF-8 html files
How does php generate UTF-8 html files ?? Note that not simply adding , But after the file is generated, use notepad to open and save the file, and the encoding is UTF-8 automatically.

------ Solution --------------------
PHP itself is unencoded, and all strings are generally considered as binary streams. Therefore, you only need to enter a string for the Utf-8. If the string adopts other encoding, you can use the iconv series function to convert the encoding.
------ Solution --------------------
Fwrite ($ f, "\ xEF \ xBB \ xBF". iconv ('gbk', 'utf-8', $ html ));

------ Solution --------------------
Discussion

Fwrite ($ f, "\ xEF \ xBB \ xBF". iconv ('gbk', 'utf-8', $ html ));

------ Solution --------------------
UTF-8, which is added to the html file Soon, I will give you a class that generates static html.
/****************** 8PHP static class *************/
Class Shtml
{
Var $ Templet;
Var $ DataSource;
Var $ Dir;

Var $ fileName;
Var $ mod;
Var $ handle;

Function Shtml ($ fileName = "")
{
$ This-> fileName = $ fileName;
$ This-> mod = "wb ";
$ This-> handle = false;

$ This-> Templet = "";
$ This-> DataSource = array ();
$ This-> Dir = "";
}

// Bind the data source. the parameter is an array.
Function BindData ($ arr)
{
$ This-> DataSource = $ arr;
}

// Set the file storage path
Function SetDir ($ dir)
{
$ This-> Dir = $ dir;
}
Function SetFileName ($ fileName)
{
Return $ this-> fileName = $ fileName;
}

Function GetMod ()
{
Return $ this-> mod;
}
Function SetMod ($ mod)
{
Return $ this-> mod = $ mod;
}
Function Open ()
{
If (substr ($ this-> fileName, 0, 1) = "/")
$ This-> fileName = $ _ SERVER ['document _ root']. $ this-> fileName;
If ($ this-> handle = fopen ($ this-> fileName, $ this-> mod ))
Return $ this-> handle;
Else
Return false;
}
Function Close ()
{
Return fclose ($ this-> handle );
}
Function Write ($ content)
{
Return fwrite ($ this-> handle, $ content );
}
Function MkDir ($ pathname)
{
$ CurrentPath = "";
Str_replace ("\", "/", $ pathname );
$ PathArr = split ("/", $ pathname );
If ($ pathArr [0] = "") {// use the absolute path
$ CurrentPath = $ _ SERVER ['document _ root'];
} Else {
$ CurrentPath = $ _ SERVER ['document _ root']. dirname ($ _ SERVER ['php _ SELF ']);
}
For ($ I = 0; $ I <count ($ pathArr); $ I ++ ){
If ($ pathArr [$ I] = "")
Continue;
Else
If (is_dir ($ currentPath. "/". $ pathArr [$ I])
$ CurrentPath = $ currentPath. "/". $ pathArr [$ I];
Else
Mkdir ($ currentPath = $ currentPath. "/". $ pathArr [$ I]);
}
}

// Generate static files
Function Create ()
{
$ Tmp = $ this-> Templet;
Foreach ($ this-> DataSource as $ key => $ value ){
$ Tmp = str_replace ("<FIELD _". $ key. ">", $ value, $ tmp );
}
$ This-> MkDir (dirname ($ this-> fileName ));
$ This-> Open ();
$ This-> Write ($ tmp );
$ This-> Close ();
}
}

Function CreateShtml (){
Ob_start ("callback_CteateShtml ");
}

Function callback_CteateShtml ($ buffer ){
$ Page = intval (@ $ _ REQUEST ["page"]);
$ Shtml = new Shtml ();
$ Shtml-> SetFileName ($ _ SERVER ['document _ root']. dirname ($ _ SERVER ['php _ SELF ']). "/". basename ($ _ SERVER ['php _ SELF '], ". php "). ($ page = 0? "": "_". Strval ($ page). ". html ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.