当我更换é时,它变成了é。我在PHP和HTML中使用UTF-8编码。我希望函数返回我放入函数的字符的HTML代码。
function ctrlspecialchars($subject){
$search = array("<",">","é","à","â","ä","æ","ç","è","ê","ë","ï","î","ò","ó","ô","ö","ø","ù","ú","û","ü","©","®","«","»","¼","½","¾","±","§","¥","£","{","}","$","%");
$replace = array("<",">","é","à","â","ä","æ","ç","è","ê","ë","ï","î","ò","ó","ô","ö","ø","ù","ú","û","ü","©","®","«","»","¼","½","¾","±","§","¥","£","{","}","$","%");
return str_replace($search,$replace,$subject);
}
答案 0 :(得分:0)
尝试charset:iso-8859-1而不是UTF-8或确保在html标头中设置charset:
PHP : header('Content-type: text/html; charset=utf-8');
HTML: <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
答案 1 :(得分:0)
由于我复制了你的代码并且它在我的本地环境中工作正常,你的问题不应该在代码中,而应该是编码。
我不知道您的编码需求(可能需要在某些页面中打印一些特殊字符),但出于测试目的,请尽量保持系统的所有部分都使用UTF-8。
按照@ user2879055的答案提示,配置数据库以使用utf8_general_ci
并确保您的PHP文件以UTF-8编码。
请参阅下面我的编辑器(我正在使用Atom)配置为以UTF-8编码文件的图像。大多数编辑器都有类似的配置。如果您的编辑器有两个UTF-8选项(有和没有BOM),请选择不带BOM(信息here)。
最后,如果您使用的是Apache,请将行IndexOptions +Charset=UTF-8
添加到.htacces
。
在这些测试中避免使用utf8_enconding
和utf8_decode
函数。
当它工作时,更改每个步骤的enconding配置并进行测试,这样当代码再次失败时,您将找到问题所在。现在,您可以根据您的需求配置您的环境,并仅针对有问题的部分开展工作。
只是FIY,我在var_dump(ctrlspecialchars("<ó äî ò æ ç % ®>"));
使用您的功能,我的浏览器显示/var/www/html/replace.php:10:string '<ó äî ò æ ç % ®>' (length=61)
。