【问题标题】:How to remove special character from the JSON如何从 JSON 中删除特殊字符
【发布时间】:2026-01-04 09:45:02
【问题描述】:

我有以下字符串 int MySQL db。

Fireman�s drop

如何去除这类特殊字符?

我尝试使用以下字符集设置标题。当我将此字符串编码为 JSON 时,它显示 null 作为值

PHP 代码

header('Content-Type: text/html; charset=ISO-8859-1');
....
$details='Fireman�s drop';//get from the db
$json_response=json_encode($details);
echo $json_response ;

输出

null

预期

Fireman's drop

如何从 JSON 中删除特殊字符?

【问题讨论】:

  • 尝试查询("SET NAMES utf8");
  • charset=utf-8 和 @SurabhilSergy 在您选择查询之前提出建议
  • 如果我设置了 UTF-8,那么它会给出 null 值

标签: php mysql json special-characters


【解决方案1】:

PHP 中的 JSON 仅支持 UTF-8,如果给定一个无效的 UTF-8 字符串(如果它包含来自 ISO-8859-1 的特殊字符,则会返回 false)。

你应该输出json_encode(utf8_encode($details))

【讨论】:

  • 它给出 null 作为输出