PHP 中具有奇怪字符集行为的函数参数
我有以下 PHP 代码:
<?php
ini_set( 'default_charset', 'UTF-8' );
mb_internal_encoding('UTF-8');
function labelValidate($label)
{
echo mb_detect_encoding($label.'x', 'UTF-8, ISO-8859-1');
echo '<br />';
echo mb_detect_encoding('Rio, coração do Brasil', 'UTF-8, ISO-8859-1');
}
labelValidate('Rio, coração do Brasil');
?>
我得到以下输出:
ISO-8859-1 UTF-8
有人知道为什么参数 $label 具有 ISO 字符集,但我在函数内创建的直接 echo 具有 UTF-8 编码?
这让我很头疼,因为我必须在 $label var 中放入 utf8_encode,否则我会在输出中得到有问题的字符。
感谢您的帮助!
i have the following PHP code:
<?php
ini_set( 'default_charset', 'UTF-8' );
mb_internal_encoding('UTF-8');
function labelValidate($label)
{
echo mb_detect_encoding($label.'x', 'UTF-8, ISO-8859-1');
echo '<br />';
echo mb_detect_encoding('Rio, coração do Brasil', 'UTF-8, ISO-8859-1');
}
labelValidate('Rio, coração do Brasil');
?>
I get this output:
ISO-8859-1
UTF-8
Anybody knows why the parameter $label has ISO charset, but the direct echo which i created inside the function has UTF-8 encode?
This is generating a headache to me, because i have to put a utf8_encode in $label var, otherwise i get problematic chars in the output.
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是关于你的文本编辑器的。
尝试从代码中删除“Rio, coração do Brasil”字符串,然后再写入一次。
It's about your text editor.
Try to delete both 'Rio, coração do Brasil' strings from code and write it one more time.
在@Marc B和@Michael Madsen的帮助下,我下载了Notepad++(PHP IDE)并将文件转换为无BOM的UTF-8字符集,所以我的所有问题都得到了解决。 Dreamweaver 将一些文件保存为 ANSI,这导致 PHP 上出现混乱。
非常感谢大家!
With the help of @Marc B and @Michael Madsen, i downloaded Notepad++ (PHP IDE) and converted the file UTF-8 charset without BOM, so all my problems have been resolved. The Dreamweaver was saving some files as ANSI, this was generationg the confusion on PHP.
Many thanks for all!