str_replace = acute(´)时输出错误在utf-8网站


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

我试图在输入表单并提交后将字符串中的撇号(')替换为急号(')。

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

字符串例如:"Jan's Motel">应该变成"Jan's Motel"

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

I 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);