From gb to utf-8_experience exchange

WBOY
Release: 2016-05-16 12:10:04
Original
1728 people have browsed it

Now the encoding of many Chinese websites is converting from gb2312 to utf-8 encoding. There are many problems that arise and are summarized here:

Prerequisite:

Ensure that all files are saved in UTF-8 encoding, not ANSI

Specific settings: If you are using a text editor, there is an encoding selection when saving the file (Figure 1) 文本编辑器的保存对话框 Just select it as UTF-8.

If you are using Dreamweaver, it will be a little more troublesome.

When using Dreamweaver to edit static files, change the charset tag of the file to utf-8: <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> to <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> and then select Save As. Dream will save the file as utf-8 encoded

Note: If you are using the access database, you only need the above two steps. Access itself uses UTF-8 encoding.

asp articles

Code page setting:

The first line of the ASP file: <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> changed to: <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> This also directly determines the encoding used by ASP to access the database through ADO.
Note: If you are using the access database, you only need the above two steps. Access itself uses UTF-8 encoding.

If a template program is used

The usual template program reads and writes files through the fso object. However, this method cannot support utf-8 and needs to be changed to a stream object to read and write files. Here are a few key lines of code:
Use stram to read files
Set objStream = Server.CreateObject(" ADODB.Stream") <code>Set objStream = Server.CreateObject("ADODB.Stream") <br>    With objStream <br>    .Open <br>    .Charset = "utf-8" <br>    .Position = objStream.Size <br>    .LoadFromFile server.mappath("sc.htm")<br>    wstr = .ReadText<br>    .Close <br>    End With <br>Set objStream = Nothing<br>  With objStream Set objStream = Server.CreateObject("ADODB.Stream") <br>    With objStream <br>    .Open <br>    .Charset = "utf-8" <br>    .Position = objStream.Size <br>    .WriteText=wstr <br>    .SaveToFile server.mappath("wz/sc_" & classid & ".htm"),2  <br>    .Close <br>    End With <br>Set objStream = Nothing .Open

.Charset = "utf-8"

.Position = objStream.Size

.LoadFromFile server.mappath("sc.htm")

wstr = .ReadText

.Close

End With <?php<BR>$conn = new COM("ADODB.Connection",NULL,65001) or die("ADO connect failed!");<BR>$rs = new COM("ADODB.RecordSet") or die("ADO recordset fail"); $conn->open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data.mdb"); <br>$rs = $conn->Execute("select * from data");<br>while not $rs->EOF<br>echo $rs->Fields['title']->value;<br>?>Set objStream = Nothing

Use stream to write files Set objStream = Server.CreateObject("ADODB.Stream" ) With objStream <p> .Open </p> .Charset = "utf-8" <code><?php mysql_query("SET NAMES 'utf8'"); ?> .Position = objStream.Size .WriteText=wstr .SaveToFile server.mappath("wz/ sc_" & classid & ".htm"),2
.Close

End With Set objStream = Nothing php article

php and access connection:


Use the COM extension of PHP on the Windows host to connect to the access database. When establishing ADODB.Connection, specify the code page parameter as 65001
<?php<BR>$conn = new COM(" ADODB.Connection",NULL,65001) or die("ADO connect failed!");</P>$rs = new COM("ADODB.RecordSet") or die("ADO recordset fail"); $conn-> open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data.mdb");

$rs = $conn->Execute("select * from data");

while not $rs ->EOF

echo $rs->Fields['title']->value;

?>

php and mysql connection

After establishing a connection with the mysql database, first execute the following SQL query: <?php<BR>require_once('adodb/adodb.inc.php'); <BR>$conn =& ADONewConnection('ado_access'); <BR>$conn->charPage ='65001';<br>//$conn->charPage = 65001;<br>//$conn->debug = true;<br>$dsn = sprintf("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= %s",realpath('2data.mdb'));<br>// $dsn = sprintf("Driver={Microsoft Access Driver (*.mdb)};Dbq=%s",realpath("data.mdb"));<br>$conn->Connect($dsn); mysql manual Description of set names SET NAMES displays what character set is used in the SQL statement sent by the client. Therefore, the SET NAMES 'cp1251' statement tells the server that "future information from this client will use character set cp1251." It also specifies the character set for the results that the server sends back to the client. (For example, if you use a SELECT statement, it indicates what character set is used for the column values.) The SET NAMES 'x' statement is equivalent to these three statements: mysql> SET character_set_client = x; Use adodb class: Set the 'charpage' attribute to 65001; <?php<🎜>require_once('adodb/adodb.inc.php'); <🎜>$conn =& ADONewConnection('ado_access') ; <🎜>$conn->charPage ='65001';//$conn->charPage = 65001;//$conn->debug = true;$dsn = sprintf ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= %s",realpath('2data.mdb'));// $dsn = sprintf("Driver={Microsoft Access Driver (*.mdb )};Dbq=%s",realpath("data.mdb"));$conn->Connect($dsn);
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template