Php Chinese character transcoding GBK to Big5 encoding conversion
- // Php Chinese character transcoding
- Class qswhBig5 {
- Var $ qswhData;
- Function qswhBig5 ($ filename = "qswhBig5.php "){
- $ This-> qswhData = file ($ filename );
- }
- Function qswhStr ($ gb, $ fail = "?? "){
- /***** (Qiushuiwuhen 2002-9-6 )******/
- $ Ret = "";
- For ($ I = 0; $ I If ($ p = ord (substr ($ gb, $ I, 1)> 127 ){
- $ Q = ord (substr ($ gb, ++ $ I, 1 ));
- $ Tmp = $ this-> qswhData [$ p-128];
- For ($ j = 0; $ j = $ Q) break;
- If ($ k = $ q) $ q = chr (hexdec (substr ($ tmp, $ j + 2, 2 ))). chr (hexdec (substr ($ tmp, $ j + 4, 2 )));
- Else if ($ fail = "") $ q = chr ($ p). chr ($ q); else $ q = $ fail;
- }
- Else
- $ Q = chr ($ p );
- $ Ret. = $ q;
- }
- Return $ ret;
- }
- Function qswhFile ($ filename, $ fail = "?? "){
- /***** (Qiushuiwuhen 2002-9-6 )******/
- If (! File_exists ($ filename. ". qswh") copy ($ filename, $ filename. ". qswh ");
- $ Fp = fopen ($ filename, "r + ");
- $ Tmp = fread ($ fp, filesize ($ filename ));
- Rewind ($ fp );
- Fwrite ($ fp, $ this-> qswhStr ($ tmp, $ fail ));
- Fclose ($ fp );
- }
-
- Function qswhDir ($ dirname, $ fail = "?? "){
- /***** (Qiushuiwuhen 2002-9-6 )******/
- $ D = dir ($ dirname );
- While ($ entry = $ d-> read ()){
- If ($ entry = "." | $ entry = "...") continue;
- $ Entry = $ dirname. "/". $ entry;
- If (is_dir ($ entry ))
- $ This-> qswhDir ($ entry, $ fail );
- Else {
- If (substr ($ entry,-5 )! = ". Qswh") $ this-> qswhFile ($ entry, $ fail );
- }
- }
- $ D-> close ();
- Return $ c;
- }
-
- }
Example:
$ Words = "no hate in autumn ";
- $ Qswh = new qswhBig5 ("qswhBig5.php"); // qswhBig5.php, which saves parameters
Echo (" Please select the encoding Big5 to view :". $ qswh-> qswhStr ($ words); <li> echo ("/n encode a directory :". $ qswh-> qswhDir ("test"); <li> echo ("/n encode a File :". $ qswh-> qswhFile ("index.htm ")); </p> <li> </ol> </p> <em onclick = "copycode ($ ('code _ L1e ')); "> </em> </p> <p> Note: The second parameter in qswhStr indicates the word that will be replaced if no corresponding Big5 word exists. if it is null, the word is retained. After formatting the file, remember to modify the content-type in <meta to Big. </P> </td> </tr> </table> </p> <p id = "comment_46681" class = "cm"> </p> <p id = "post_rate_p_46681">
|