gpt4 book ai didi

php - 哪些因素会影响成功的 iconv() TRANSLIT 转换?

转载 作者:可可西里 更新时间:2023-10-31 22:15:36 25 4
gpt4 key购买 nike

我正在尝试确定哪些环境或其他因素对使用 TRANSLIT 选项调用 iconv() 的结果有影响。

当通过 Apache 和 CLI 运行时,以下代码在本地对我有不同的结果。

<?php
setlocale(LC_ALL, 'en_GB.UTF-8');
header('Content-type: text/html; charset=utf-8'); // for web

$utf8_string = "Pádraig's naïve café";

echo iconv('UTF-8', 'ASCII//IGNORE//TRANSLIT', $utf8_string);
?>

预期结果:Padraig's naive cafe

Web 浏览器中的结果:(空字符串)

CLI 的结果:P'adraig's naive cafe

在某些系统上,我确实得到了预期的结果,但我无法确定原因。

哪些因素会影响转化,我应该遵循哪些步骤来增加获得良好结果的机会?

最佳答案

语言环境正在影响iconv音译。但是,您应该阅读 setlocale­Docs 上的警告手册页:

The locale information is maintained per process, not per thread. If you are running PHP on a multithreaded server API like IIS or Apache on Windows, you may experience sudden changes in locale settings while a script is running, though the script itself never called setlocale(). This happens due to other scripts running in different threads of the same process at the same time, changing the process-wide locale using setlocale().

因此您可能设置了语言环境,但它在其他地方发生了变化。只要语言环境完全相同,您就会得到相同的结果。

您可以在此处找到 iconv 的文档和源代码:http://www.gnu.org/software/libiconv/ - 这通常是 PHP 使用的库。

关于php - 哪些因素会影响成功的 iconv() TRANSLIT 转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9771886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com