首页 > 解决方案 > html输入值内容溢出

问题描述

假设在输入元素内的文本部分地在该元素输入字段之外。

值内容是一段 html 代码。(见下文)

输入的值是在使用给定的代码段作为值的输入元素创建时设置的。

<?php
$body_content = '' //the variable is set with the value posted bellow but is the result of a a $var['field'] of a mysqli_fetch_assoc of a mysqli_query

echo ("
<div>
  <label>Body<input type='text' name='body' required value='$body_content'></label>
</div>
");
?>

从db中提取的文本如下:

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p><h2 class="section-heading">Heading</h2><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p><figure><blockquote class="blockquote"><p class="mb-0">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p></blockquote></figure><h2 class="section-heading">Heading</h2><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p><a href="#"><img class="img-fluid" src="assets/img/post_sample_image.jpg"></a><span class="text-muted caption">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</span><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p><p><span>Placeholder text by&nbsp;</span><a href="http://spaceipsum.com">Ipsum</a><span>&nbsp;Photographs by&nbsp;</span><a href="https://www.flickr.com/photos/nasacommons/">Lorem</a>.</p>

我怀疑问题的原因是特殊字符。

我想知道是否有更简单的选择来解决这个问题。

我也需要那些特殊字符。

标签: phphtml

解决方案


您需要转义 html 特殊字符(可能使用htmlspecialchars):

echo("
<div>
  <label>Body<input type='text' name='body' required value='" . htmlspecialchars($body_content, ENT_QUOTES) . "'></label>
</div>
");
?>

推荐阅读