gpt4 book ai didi

php - 为什么 htmlentities 在每行末尾的文本区域内添加 2 个额外的新行?

转载 作者:行者123 更新时间:2023-11-29 00:17:11 24 4
gpt4 key购买 nike

我有一个带有文本区域的表单,用户可以在其中写下他们自己。我正在使用 PHP 处理表单并将 textarea 的值存储在 Mysql 数据库中。当我从数据库中检索值以在文本区域中再次输出它以便用户可以编辑他们写的内容时,我正在使用 htmlentities 函数来编码特殊的 html 字符。问题是这个函数向它找到的每个\r\n 添加了 2 个额外的新行。例如,如果我在数据库中的值(value)是

Hello,My name is ___.I am a ______ .

When I use htmlentities to output it to the textarea what is being shown is this:

Hello,My name is ____ .I am a _______ .

When I print the value inside the textarea this is the function I'm using:

function profile_input_about( $profile_obj = false ) {
$profile_about = ( $profile_obj ) ? htmlentities( $profile_obj->about, ENT_QUOTES | ENT_HTML5, 'UTF-8' ) : '';

print $profile_about;
}

如果我避免使用 htmlentities 或改为使用 htmlspecialchars,则不会发生这种情况。有什么线索吗?

更新

我做了一个单机测试:

<?php
$text = "Hello there,\r\nThis is a test.\r\nWhat do you see?";
?>
<!DOCTYPE html>
<html>
<body>
<textarea><?php print htmlentities( $text, ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ?></textarea>
<textarea><?php print htmlspecialchars( $text, ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ?></textarea>
</body>
</html>

htmlentities() 绝对添加一个额外的\n。

结果是:

Hello there,This is a test.What do you see?

Hello there,This is a test.What do you see?

如果我不使用 ENT_HTML5 标志,问题就会消失。它按原样显示文本。

这应该是这样还是这是一个 php 错误?

最佳答案

已编辑 -

根据文档,使用 htmlentities , “所有具有 HTML 字符实体等价物的字符都被翻译成这些实体。”意思是,\r被解释为具有相当于 \n 的 HTML 字符实体, 因此你有两条新线。

htmlspecialchars ,只有引号之类的字符,</> , &被翻译。 \r\n仍然存在,导致每行文本之间只有一个新行标记的预期结果。

关于php - 为什么 htmlentities 在每行末尾的文本区域内添加 2 个额外的新行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22562413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com