Home > Backend Development > PHP Problem > What should I do if the name of the new php file is garbled?

What should I do if the name of the new php file is garbled?

藏色散人
Release: 2023-03-17 11:28:01
Original
1619 people have browsed it

Solution to the garbled name of a new php file: 1. Use the "iconv" or "mb_convert_encoding" function to convert the character encoding; 2. Use the "function convertEncoding($string){...}" method to implement the Just set the system settings to convert the encoding.

What should I do if the name of the new php file is garbled?

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

php What should I do if the name of the new file is garbled?

Under Windows, when PHP calls the mkdir(), file_put_contents(), and fopen() functions to create a directory or document name with Chinese characters, garbled characters appear.

Cause

After consulting the information, this is related to the system character set.

  • windows (Simplified Chinese), the default character set is: gbk

  • windows (Traditional Chinese), the default character set is: big5

  • linux, the default character set is: utf-8

Solution

Use iconv or mb_convert_encoding function Convert character encoding.

//将字符串 str 从 in_charset 转换编码到 out_charset。
string iconv ( string $in_charset , string $out_charset , string $str )
//将 string 类型 str 的字符编码从可选的 from_encoding 转换到 to_encoding。
string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding = mb_internal_encoding() ] )
Copy after login

Convert encoding according to system settings, the method is as follows:

<?php
/**
 * 转换字符编码
 * @param $string
 * @return string
 */
function convertEncoding($string){
    //根据系统进行配置
    $encode = stristr(PHP_OS, &#39;WIN&#39;) ? &#39;GBK&#39; : &#39;UTF-8&#39;;
    $string = iconv(&#39;UTF-8&#39;, $encode, $string);
    //$string = mb_convert_encoding($string, $encode, &#39;UTF-8&#39;);
    return $string;
}
Copy after login

Garbled code example

<?php
#示例1
$dir = __DIR__ . &#39;/中英文目录Test1&#39;;
mkdir($dir); 
//目录名:涓嫳鏂囩洰褰昑est1
#示例2
$fileName = __DIR__ . &#39;/中英文档名Test2.txt&#39;;
file_put_contents($fileName, &#39;文件内容Test2&#39;);
//文件名:涓嫳鏂囨。鍚峊est2.txt
//文件内容:文件内容Test2
#示例3
$fileName = __DIR__ . &#39;/中英文档名Test3.txt&#39;;
$fp = fopen($fileName, &#39;w&#39;);
fwrite($fp, &#39;文件内容Test3&#39;);
fclose($fp);
//文件名:涓嫳鏂囨。鍚峊est3.txt
//文件内容:文件内容Test3
?>
Copy after login

Solution example

<?php
#示例1
$dir = __DIR__ . &#39;/中英文目录Test1&#39;;
$dir = convertEncoding($dir);
mkdir($dir); 
//目录名:中英文目录Test1
#示例2
$fileName = __DIR__ . &#39;/中英文档名Test2.txt&#39;;
$fileName = convertEncoding($fileName);
file_put_contents($fileName, &#39;文件内容Test2&#39;);
//文件名:中英文档名Test2.txt
//文件内容:文件内容Test2
#示例3
$fileName = __DIR__ . &#39;/中英文档名Test3.txt&#39;;
$fileName = convertEncoding($fileName);
$fp = fopen($fileName, &#39;w&#39;);
fwrite($fp, &#39;文件内容Test3&#39;);
fclose($fp);
//文件名:中英文档名Test3.txt
//文件内容:文件内容Test3
/**
 * 转换字符编码
 * @param $string
 * @return string
 */
function convertEncoding($string){
    //根据系统进行配置
    $encode = stristr(PHP_OS, &#39;WIN&#39;) ? &#39;GBK&#39; : &#39;UTF-8&#39;;
    $string = iconv(&#39;UTF-8&#39;, $encode, $string);
    //$string = mb_convert_encoding($string, $encode, &#39;UTF-8&#39;);
    return $string;
}
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What should I do if the name of the new php file is garbled?. For more information, please follow other related articles on the PHP Chinese website!

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