无法将 POST 请求中的智能引号替换为 PHP

Can't replace a smart quote in POST request with PHP

我无法使用 str_replace 单个 'smart' 引号 ('),但前提是它作为 $_POST 请求传递。

确切的问题是我的客户正在从浏览器中复制和粘贴,其中引号是从 ’ 呈现的。当他将文本粘贴到表单中时,它会更新数据库条目,只要弯引号在数据库中,整个站点就会中断。我没有创建他的网站,所以很难尝试找出这个问题的原因,但我确实将其缩小到复制和粘贴大引号。因此,我的第一个简单解决方案是在它们过来后立即更换它们 POST.

可以在这里看到一个例子:

http://wheatbeakinc.com/quote.php

这是确切的源代码:

<div style="font-size:30px;">

<?php

if(isset($_POST["text"])){
    
    $foo = str_replace("’","'","tes’t");
    
    $chr_map = array(
   // Windows codepage 1252
   "\xC2\x82" => "'", // U+0082⇒U+201A single low-9 quotation mark
   "\xC2\x84" => '"', // U+0084⇒U+201E double low-9 quotation mark
   "\xC2\x8B" => "'", // U+008B⇒U+2039 single left-pointing angle quotation mark
   "\xC2\x91" => "'", // U+0091⇒U+2018 left single quotation mark
   "\xC2\x92" => "'", // U+0092⇒U+2019 right single quotation mark
   "\xC2\x93" => '"', // U+0093⇒U+201C left double quotation mark
   "\xC2\x94" => '"', // U+0094⇒U+201D right double quotation mark
   "\xC2\x9B" => "'", // U+009B⇒U+203A single right-pointing angle quotation mark

   // Regular Unicode     // U+0022 quotation mark (")
                          // U+0027 apostrophe     (')
   "\xC2\xAB"     => '"', // U+00AB left-pointing double angle quotation mark
   "\xC2\xBB"     => '"', // U+00BB right-pointing double angle quotation mark
   "\xE2\x80\x98" => "'", // U+2018 left single quotation mark
   "\xE2\x80\x99" => "'", // U+2019 right single quotation mark
   "\xE2\x80\x9A" => "'", // U+201A single low-9 quotation mark
   "\xE2\x80\x9B" => "'", // U+201B single high-reversed-9 quotation mark
   "\xE2\x80\x9C" => '"', // U+201C left double quotation mark
   "\xE2\x80\x9D" => '"', // U+201D right double quotation mark
   "\xE2\x80\x9E" => '"', // U+201E double low-9 quotation mark
   "\xE2\x80\x9F" => '"', // U+201F double high-reversed-9 quotation mark
   "\xE2\x80\xB9" => "'", // U+2039 single left-pointing angle quotation mark
   "\xE2\x80\xBA" => "'", // U+203A single right-pointing angle quotation mark
);
$chr = array_keys  ($chr_map); // but: for efficiency you should
$rpl = array_values($chr_map); // pre-calculate these two arrays
$bar = str_replace($chr, $rpl, html_entity_decode($_POST["text"], ENT_QUOTES, "UTF-8"));
        
        echo "foo: " . $foo . " - <em>shows straight quote (for me)</em><br /><br >";
        echo "bar: " . $bar . " - <em>still shows curly quote (for me)</em><br /><br >";    
        
}

?>


Copy this into the input: tes&rsquo;t

<form action="" method="post">

<input type="text" name="text" />
<br>
<br>
<input type="submit" value="Submit" />

</form>

</div>

如果我在表单中填写完全相同的字符串 (tes’) 并点击提交,它会给出以下结果:

foo: tes't

bar: tes’t

即使字符串相同,通过 post 的字符串也不会被替换。有谁知道为什么会这样?

这不是另一个问题的重复,该解决方案不起作用。

经过测试(我怀疑这是一个编码问题;我不小心删除了我对此的评论),我能够找出您的代码失败的原因。

这是因为您的文件编码可能设置为 UTF-8 没有 BOM。

如果是这种情况,请将其更改为 with BOM(字节顺序标记),它将按预期工作。

参考:


备注:

将文件保存为 ANSI 编码,也确实用常规引号替换了弯引号,因此您可以选择。作为 ANSI,或带 BOM 的 UTF-8。

您可以为此使用 Notepad++ 等编辑器。

从下拉菜单中,您可以选择:

  • 编码,转换为带BOM的UTF-8,然后保存。
  • 或者,编码,转换为 ANSI,然后保存。
  • 选择权在你。

重要旁注: 不要选择 "Encode in...",因为一旦您保存它就不会转换您的文件。您必须选择 "Convert to".

您可以使用其他代码编辑器来获得相同的结果。