PHP 字符编码问题 ™ 打印为 ™
PHP character encoding issue ™ prints as â„¢
我正在从 URL 中获取一些 json 数据,如下所示:
https://steamcommunity.com/id/itemdepot3/inventory/json/730/2
$json = json_decode(file_get_contents(
"https://steamcommunity.com/id/itemdepot3/inventory/json/730/2"),true);
然后我尝试打印每个项目的名称:
foreach($json["rgInventory"] as $item){
$cid = $item["classid"];
$iid = $item["instanceid"];
$name = $json["rgDescriptions"]["{$cid}_{$iid}"]["market_name"];
echo $name;
}
但是应该包含 ™ 的名称会像这样被破坏:
StatTrakâ„¢ Five-SeveN | Nightshade (Field-Tested)
我尝试了一堆愚蠢的正则表达式修复(因为这是我需要修复的唯一字符)但无济于事。我不知道我在这里处理的是哪种编码,我所有的搜索都没有结果。如何让我的 ™ 符号正确打印?
如以上评论所述,您需要确保指定正确的输出编码。
问题源于您输出的是 UTF-8 字符,但您的网络浏览器试图将页面解码为 single-byte 字符集(很可能是 ISO-8859-1 或 Windows-1252).
确保始终通过适当使用 HTTP headers(在本例中为 Content-Type: text/html; charset=utf-8
)或通过使用 <meta>
标记来指定输出编码。
我正在从 URL 中获取一些 json 数据,如下所示: https://steamcommunity.com/id/itemdepot3/inventory/json/730/2
$json = json_decode(file_get_contents(
"https://steamcommunity.com/id/itemdepot3/inventory/json/730/2"),true);
然后我尝试打印每个项目的名称:
foreach($json["rgInventory"] as $item){
$cid = $item["classid"];
$iid = $item["instanceid"];
$name = $json["rgDescriptions"]["{$cid}_{$iid}"]["market_name"];
echo $name;
}
但是应该包含 ™ 的名称会像这样被破坏:
StatTrakâ„¢ Five-SeveN | Nightshade (Field-Tested)
我尝试了一堆愚蠢的正则表达式修复(因为这是我需要修复的唯一字符)但无济于事。我不知道我在这里处理的是哪种编码,我所有的搜索都没有结果。如何让我的 ™ 符号正确打印?
如以上评论所述,您需要确保指定正确的输出编码。
问题源于您输出的是 UTF-8 字符,但您的网络浏览器试图将页面解码为 single-byte 字符集(很可能是 ISO-8859-1 或 Windows-1252).
确保始终通过适当使用 HTTP headers(在本例中为 Content-Type: text/html; charset=utf-8
)或通过使用 <meta>
标记来指定输出编码。