在 utf-8 网站中 str_replace 带有尖音符 ( ´ ) 时输出错误

Wrong output when str_replace with acute ( ´ ) in utf-8 website

我正在尝试在输入表单并提交后将字符串中的撇号 (') 替换为尖音符 (´)。

<?= str_replace("'","´",$_POST['string']) ?>
例如

string 是:"Jan's Motel" > 应该变成 "Jan´s Motel"

这在使用字符集 iso-8859-1 时效果很好,但我需要我的网站使用 utf-8。

我utf-8结果字符串是"Jan´s Motel"

我不明白为什么它变成了“´”而不是“´”

这是我的示例代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>notitle</title>
  </head>

  <body>
    <form action="?" method="post">
      <input type="text" name="string" value="<?= str_replace("'","´",$_POST['name']) ?>" />
    </form>
  </body>
</html>

有人可以帮忙吗?

尝试utf8_decode('')

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>notitle</title>
  </head>

  <body>
    <form action="?" method="post">
      <input type="text" name="string" value="<?= str_replace("'",utf8_decode('`'),$_POST['name']) ?>" />
    </form>
  </body>
</html>

你可以使用header("Content-type: text/html; charset=utf-8");或 HTML 标签。

header("Content-type: text/html; charset=utf-8");
$string = "My name is Jan's";
echo str_replace("'", "´", $string);