Home >Backend Development >PHP Problem >What should I do if the name of the new php file is garbled?
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.
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() ] )
Convert encoding according to system settings, the method is as follows:
<?php /** * 转换字符编码 * @param $string * @return string */ function convertEncoding($string){ //根据系统进行配置 $encode = stristr(PHP_OS, 'WIN') ? 'GBK' : 'UTF-8'; $string = iconv('UTF-8', $encode, $string); //$string = mb_convert_encoding($string, $encode, 'UTF-8'); return $string; }
Garbled code example
<?php #示例1 $dir = __DIR__ . '/中英文目录Test1'; mkdir($dir); //目录名:涓嫳鏂囩洰褰昑est1 #示例2 $fileName = __DIR__ . '/中英文档名Test2.txt'; file_put_contents($fileName, '文件内容Test2'); //文件名:涓嫳鏂囨。鍚峊est2.txt //文件内容:文件内容Test2 #示例3 $fileName = __DIR__ . '/中英文档名Test3.txt'; $fp = fopen($fileName, 'w'); fwrite($fp, '文件内容Test3'); fclose($fp); //文件名:涓嫳鏂囨。鍚峊est3.txt //文件内容:文件内容Test3 ?>
Solution example
<?php #示例1 $dir = __DIR__ . '/中英文目录Test1'; $dir = convertEncoding($dir); mkdir($dir); //目录名:中英文目录Test1 #示例2 $fileName = __DIR__ . '/中英文档名Test2.txt'; $fileName = convertEncoding($fileName); file_put_contents($fileName, '文件内容Test2'); //文件名:中英文档名Test2.txt //文件内容:文件内容Test2 #示例3 $fileName = __DIR__ . '/中英文档名Test3.txt'; $fileName = convertEncoding($fileName); $fp = fopen($fileName, 'w'); fwrite($fp, '文件内容Test3'); fclose($fp); //文件名:中英文档名Test3.txt //文件内容:文件内容Test3 /** * 转换字符编码 * @param $string * @return string */ function convertEncoding($string){ //根据系统进行配置 $encode = stristr(PHP_OS, 'WIN') ? 'GBK' : 'UTF-8'; $string = iconv('UTF-8', $encode, $string); //$string = mb_convert_encoding($string, $encode, 'UTF-8'); return $string; }
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!