php - 我可以在不破坏站点的情况下将所有 PHP 非多字节函数更改为它们的 mb_ 等效函数(即 mb_str_replace、mb_strlen 等)吗?
问题描述
我需要升级一个大型 PHP 站点以支持 UTF-8... 作为第一步,我想更改所有 PHP 代码以使用 mb_ 函数。我现在可以继续做吗,即使没有其他任何东西变成多字节?(即它不会破坏任何东西,对吧?)
在继续下一步(升级数据库等)之前,我想先完成 PHP 的工作,并让该代码在我现有的非多字节站点上运行并运行。
解决方案
您可以使用自定义函数来获得依赖于其他机制的结果。我建议5种方法:
1) 读取旧字符串并将其转换为 8bit 的自定义函数get_string_utf8()
。用法很简单:
function get_string_utf8($string) {
return mb_convert_encoding($string, 'UTF-8', mb_detect_encoding($string, 'UTF-8, ISO-8859-1', true));
}
$old_string = "Elämä on kaunis ja mahtavia yllätyksiä"; //Life is beautiful and can hold nice surprises
$new_string = get_string_utf8($old_string);
2) 读取旧文件并使用 unicode (8bit) 打开它的自定义函数file_get_contents_utf8()
。用法很简单:
function file_get_contents_utf8($file) {
$content = file_get_contents($file);
return mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
}
$old_file = "Life_is_beautiful.html";
$new_file = file_get_contents_utf8("$old_file");
3) 始终使用:<?php ini_set('default_charset', 'UTF-8'); ?>
在您的 php 文件的开头。
4)如果可能的话,您应该通过将(通过 php 写入过程)一个 8 位文件(没有 BOM)保存为 unicode (UTF-8) 来直接提供一个 8 位文件
5)始终使用正确的元:<meta charset="UTF-8">
我希望这有帮助。
推荐阅读
- vue.js - 如何在 Vue 应用程序中触发重新计算?
- angular - Angular 2: Http get call with Subject not working as expected
- c# - 未在 Xamarin.CarouselView 中显示图像
- python - PyMc3:后验预测检查导致错误
- python - 如何创建通过迭代运行的累积代码?
- linux - linux下shebang不拆分参数
- docker-compose - 我如何在 haproxy 后面使用 wso2
- angular6 - Angular 6:在生产中禁用管道转换
- python - n 个长度为 l 的项目的所有组合,其中 n < l
- python-3.6 - python 3编程彩票号码生成器