首页 > 解决方案 > 如何正确地将外语名称编码为 php 中 .txt 文件的名称

问题描述

我想将人名编码为.txt文件名

现在我使用下面的代码将名称设为小写,去掉空格并且没有重音字符:

$raw_authorname = 'Charlène Rodriës';

$stripped_authorname = preg_replace("/[^a-z.]+/i", "", $raw_authorname); // strip accented chars out authorname
$stripped_authorname = preg_replace('/\s+/', '', $stripped_authorname); // strip out spaces
$stripped_authorname = strtolower($stripped_authorname); // make lowercase

echo $stripped_authorname; // output: charlnerodris
//And the txt file will be: charlnerodris.txt

这适用于拉丁字母。当名称例如是俄罗斯名称时会出现问题:Юлия Преснякова

我无法.txt使用此名称创建文件:Юлия Преснякова.txt 阿拉伯名称存在相同问题。所以我认为; 也许最好对名称进行编码并.txt使用编码名称创建一个文件。对这些名称进行编码的最合适方法是什么?或者有没有更好的方法来相应地更改名称,我可以始终为 txt 文件创建一个名称?

标签: phpencoding

解决方案


推荐阅读