php 带有特殊字符,如 ?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13208149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:58:25  来源:igfitidea点击:

php with special characters like ?

phputf-8headerspecial-charactersjson

提问by Fred

At first I thought the problem was when I return echo json_encode($row) from an ajax call that results with ? are changed to NULL. But after testing I found out that the problem exists way before that.

起初我认为问题是当我从结果为 ? 更改为 NULL。但是经过测试,我发现问题在那之前就存在了。

In a sample php file with:

在一个示例 php 文件中:

$test = "Nu?ez"
echo $test

the result is just Nu?ez

结果只是 Nu?ez

I've searched around but none of the suggested solutions work. Like:

我四处搜索,但没有一个建议的解决方案有效。喜欢:

mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');
mb_language('uni');
mb_regex_encoding('UTF-8');
ob_start('mb_output_handler');

or <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />, or header('content-type: text/html; charset: utf-8');. And some more solutions that I've already forgotten, believe me I tried a lot.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />, 或header('content-type: text/html; charset: utf-8');。还有一些我已经忘记的解决方案,相信我,我尝试了很多。

That's just the beginning of it, I'm hoping that won't be a problem with mysql since my database is in utf-8 so is my $mysqli charset. But I think I can't say the same for ajax json_encode. But nevermind, one problem at a time. Can anybody please help me. Thanks a lot!

这只是它的开始,我希望 mysql 不会有问题,因为我的数据库是 utf-8,所以我的 $mysqli 字符集也是如此。但我想我不能对 ajax json_encode 说同样的话。但没关系,一次一个问题。任何人都可以帮助我。非常感谢!

PROBLEM SOLVEDI just had to set "Encode in UTF-8" in Notepad++, as it was in "Encode in ANSI" before.

问题已解决我只需要在 Notepad++ 中设置“以 UTF-8 编码”,就像之前在“以 ANSI 编码”中一样。

采纳答案by 000

for me

为了我

$test = "Nu?ez";
echo $test;

shows Nu?ez

显示Nu?ez

You may try

你可以试试

$test = "Nu?ez";
echo utf8_decode($test);

or

或者

$test = utf8_encode("Nu?ez");
echo utf8_decode($test);

回答by Hkachhia

Try this

尝试这个

Its works for me.

它对我有用。

$test = "Nu?ez";

echo html_entity_decode(htmlentities($test));

回答by Pablo Garcia

to show correctly latin characters like ? ? á é í ó ú, etc.. in browsers, you have to use iso-8859-1 instead of UTF-8 encoding http://www.w3schools.com/tags/ref_entities.asp

正确显示拉丁字符,如?? á é í ó ú 等。在浏览器中,您必须使用 iso-8859-1 而不是 UTF-8 编码 http://www.w3schools.com/tags/ref_entities.asp

best regards!

此致!

回答by Eugen

when you connect to your mysql db, set charset to utf-8, like ->

当您连接到 mysql 数据库时,将字符集设置为 utf-8,例如 ->

$sql_con = mysql_connect($sql_host, $sql_user, $sql_pass);
mysql_query('SET NAMES utf8');

回答by Pablo Pazos

consider setting default_charset, this worked for me

考虑设置 default_charset,这对我有用

ini_set('default_charset', 'utf-8');

ini_set('default_charset', 'utf-8');

回答by itassets

Face the same issue with the string "CASTA?O".

字符串“CASTA?O”也面临同样的问题。

I've tried posted solutions.

我试过发布的解决方案。

I used the ini_set('default_charset', 'utf-8');directive.

我使用了ini_set('default_charset', 'utf-8');指令。

  • The selected record showed "CASTA?O"
  • Using utf8-decodeshowed "CASTA?O"
  • Using utf8_encodeshows the proper string "CASTA?O"
  • 所选记录显示“CASTA?O”
  • 使用utf8-decode显示 "CASTA?O"
  • 使用utf8_encode显示正确的字符串“CASTA?O”