dragon201401 2014-07-23 14:47
浏览 49
已采纳

htmlentities不转换HTML标签

When I submit the form, PHP echoes out tags exactly as I have written them instead of converting them into htmlentities. What is the problem?

PHP:

<?php

$name = htmlentities(substr($_POST["name"], 0, 100), ENT_QUOTES);
$email = htmlentities(substr($_POST["email"], 0, 100), ENT_QUOTES);
$msg = htmlentities(substr($_POST["message"], 0, 500), ENT_QUOTES);

echo ($name.'<br>'.$email.'<br>'.$msg);

HTML:

  <html>

  <body>

      <form role="form" action="test.php" method="POST">

            <p>Name:<input type="text" placeholder="Name" maxlength="100" name="name"></p>

            <p>Email address:<input type="text" placeholder="Email" maxlength="100" name="email"></p>

            <p>Message:<textarea rows="4" name="message" placeholder="Message" maxlength="500"></textarea></p>

              <input type="submit" value="submit">

        </form>

  </body>

  </html>
  • 写回答

1条回答 默认 最新

  • duanhan9479 2014-07-23 14:51
    关注
    1. You type the text <example>
    2. You submit to the PHP
    3. The PHP converts to the text &lt;example&gt;
    4. The browser gets an HTML document containing the HTML source code &lt;example&gt;
    5. The browser parses the HTML and:
      1. Treats &lt; as an instruction to display a < in the document
      2. Treats example as text to display
      3. Treats &gt; as an instruction to display a > in the document

    If htmlentities had no effect, then <example> would be treated as an unknown tag and you would see nothing.

    If you want to convert to HTML entities and then display the entities in an HTML document, then you'll need to run them through htmlentities twice.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题