yii 中的 utf8 字符和 htmlspecialchars 错误

2022-01-04 00:00:00 utf-8 character-encoding php html yii

我在 yii 中的字符编码有问题.如果我创建一个新的网络应用程序:

 ./Yii-framework/framework/yiic webapp MyTest

然后去/protected/views/layouts/main.php 将页脚更改为一个带有utf8字符的文本,例如

版权 <br/>

刷新页面,一切正常.好的!;)

然后我尝试使用用户名中的 utf8 字符登录,例如 áadmin,它崩溃说:

错误 500htmlspecialchars(): 参数中的多字节序列无效

所以我查看了这篇关于yii中的unicode

然后我转到 /protected/config/main.php 并在开头添加了这一行:

header('Content-Type: text/html; charset=utf-8');

再次重试相同的登录它可以工作(不会崩溃)但现在页脚已损坏并显示:

C.py.ght

我尝试过其他组合,如yii 中的 Unicode"一文中所述,但没有一个可以同时使这两种方法有效.

有解决这个问题的想法吗?

注意:我无法更改为 php.ini 文件.

我还尝试了 .htaccess 文件中的 AddDefaultCharset UTF-8 选项,并将其放在/MyTest/下的文件夹中,文章中提到的正确文件夹为:您的 文档根 ?

谢谢

解决方案

我对 yii 一点都不熟悉,但是,如果你想将文字 unicode 字符粘贴到文件中,你需要确保你的文本编辑器使用 unicode 编码保存文件,例如 utf8.试试 utf8,没有 BOM.

我的经验是,当您更改编码设置并且其中已经有编码字符时,文本编辑器的行为会很奇怪.只需从一个新文件重新开始,更改编码,然后将字符粘贴进去.

I have a problem with the char encoding in yii. If I create a new webapp:

 ./Yii-framework/framework/yiic webapp MyTest

Then go to /protected/views/layouts/main.php and change the footer to a text with an utf8 character, for example

<div id="footer">
        Cópyrîgth <br />
</div>

Refresh the page and everything is ok. Nice! ;)

And then I try to log in with an utf8 character in the username, for example ádmin, it crashes saying:

Error 500

htmlspecialchars(): Invalid multibyte sequence in argument

So I checked this article about unicode in yii

and then I went to /protected/config/main.php and added this line at the start:

header('Content-Type: text/html; charset=utf-8');

Retrying the same login again it works (doesn't crash) but now the footer is broken and shows:

C�pyr�ght

I've tried other combinations like explained in the "Unicode in yii" article but none of them make both things work at the same time.

Any ideas for solving this problem?

Note: I can't change to the php.ini file.

I also tried the AddDefaultCharset UTF-8 option in the .htaccess file and put it in the folder at the /MyTest/ is that the correct folder referred in the article as: your DocumentRoot ?

Thanks

解决方案

I'm not at all familiar with yii, but, if you want to paste literal unicode characters into a file, you need to make sure that your text editor saves the file using a unicode encoding, such as utf8. Try utf8, without a BOM.

My experience is that text editors behave strange when you change the encoding setting and there's already encoded characters in it. Just start over with a fresh file, change the encoding, then paste the characters in.

相关文章