【问题标题】:Escape double quotes in input value转义输入值中的双引号
【发布时间】:2014-07-10 14:31:47
【问题描述】:

我有一个输入字段,其中 PHP 中回显的值使用 UTF-8 字符集编码

<input value="<?php echo html_entity_decode($data["title"]); ?>" type="text">

样本值:

“पीला सितारा పసుపు నక్షత్రం黄星!”

文本中出现的双引号阻止了值的显示。

我已经尝试过了,但它不起作用。

<input name="title" id="title" value="<?php echo htmlspecialchars_decode(html_entity_decode($data["title"])); ?>"  class="width9" type="text"> type="text">

【问题讨论】:

  • 只要使用 htmlspecialchars($data["title"]) 就可以了。
  • htmlentities($value, ENT_COMPAT, "UTF-8") 是我们正在使用的
  • 您正在使用的功能与您想要的相反。你应该使用htmlspecialchars()
  • 使用 htmlspcialchars 我得到这个 "पीला सितार ा పసుపు నక్షత్ 3120;ం@TiMESPLiNTER
  • @tomhre:我之前尝试过 htmlentites,然后我得到了这个:“पीलासित ;ाराపసుపునక్ష& #3108;్రం

标签: php


【解决方案1】:

问题是你做错了。您尝试解码编码的字符串。但是你必须对字符串进行编码。

所以你需要

htmlspecialchars($data["title"])

而不是

htmlspecialchars_decode($data["title"])

因为htmlspecialchars_decode() 会解码一个 htmlspecialchars 编码的字符串。

【讨论】:

  • 使用 htmlspcialchars 我得到这个 "पीला सितार ा పసుపు నక్షత్ 3120;ం
  • 我希望这是我的输出:“पीला सितारा పసుపు నక్షత్రం黄星!”
  • 我不知道您使用的是哪个 PHP 版本,但对于我使用 PHP 5.5 的我来说,&amp;quot;पीला सितारा పసుపు నక్షత్రం yellow star!&amp;quot; 如果您低于 PHP 5.4,您可能会遇到字符集问题。
  • 我使用的是 5.3.10-1ubuntu3.11。我该如何克服这个问题?
  • @Paulie 我建议您升级到 PHP 5.4 或 5.5。如果这不可能,您可以查看手册页以了解 htmlspecialchars() 如何解决字符集问题。
【解决方案2】:

htmlspecialchars($string) 应该可以工作,htmlspecialchars($string,ENT_QUOTES,"UTF-8") 和 htmlentities($value, ENT_COMPAT, "UTF-8") 也应该工作

我想知道您的问题是否实际上不是您没有在最终页面中指定元字符集

一些代码供你测试

<?php

if(empty($_POST['string_to_process'])) { $string = "enter your string"; } else { $string = $_POST['string_to_process']; }

?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    </head>
    <body>
        <form action="/encoding.php" method="POST">
            <input type="text" name="string_to_process" />
            <input type="submit" value="submit">
        </form>

        RESULTS:
        WITHOUT ESCAPING : <input type="text" value="<?php echo($string) ?>" /><br />
        FOR htmlspecialchars($string): <input type="text" value="<?php echo(htmlspecialchars($string)) ?>" /><br />
        FOR htmlentities($string): <input type="text" value="<?php echo(htmlentities($string)) ?>" /><br />
        FOR htmlspecialchars($string,ENT_QUOTES,"UTF-8"): <input type="text" value="<?php echo(htmlspecialchars($string,ENT_QUOTES,"UTF-8")) ?>" /><br />
        FOR htmlentities($value, ENT_COMPAT, "UTF-8"): <input type="text" value="<?php echo(htmlentities($string, ENT_COMPAT, "UTF-8")) ?>" /><br />

    </body>
</html>

希望对老兄有帮助,编码可能会很痛苦

编辑:

是的,我只是没有尝试过

<meta http-equiv="content-type" content="text/html;charset=utf-8" />

它搞砸了,包括上面的一切都会很漂亮

TH

【讨论】:

  • 这对我不起作用。我已经定义了字符集。这是我得到的原始 JSON -> "title": "\"पीला सितारा\""
  • @Paulie 你之前没有提到过 json,你可以编辑问题并提供所有准备 json 响应的代码
猜你喜欢
  • 1970-01-01
  • 2020-11-26
  • 1970-01-01
  • 1970-01-01
  • 2013-06-17
  • 2013-08-09
  • 1970-01-01
  • 2011-06-09
  • 1970-01-01
相关资源
最近更新 更多