我查询mySql数据库表并检索如下文本字符串:
This is a piece of a bronze artifact from the 'Bronze Age' -- it's in outstanding shape.
His amusing comment on the 3rd-century clay bowl: "Wow, she's a real piece!"
It's no secret that you 'don't mishandle old parchment artifacts, they're "very" fragile' -- yet it still happens.
问题——我使用php变量来保存从数据库中读取的上述字符串,并在我的heredoc表单中使用php变量。我马上看到,当遇到字符串中的第一个空格时,字符串被截断了——表单只显示第一个空格之前的文本。
因此,我在heredoc表单中的PHP变量周围加上了单引号(见下文)。
现在,当来自数据库的字符串有单引号时,我得到了截断。
我可以在php变量周围加引号,但当单引号、双引号时,我会截断文本字符串如果我不引用heredoc中的PHP变量,字符串中的一个空格会从那时起截断字符串。
我使用的是PHP,下面的代码从mySql查询结果中获取数据,将数据库数据存储到PHP变量中,然后在heredoc:中使用它们
$row = mysql_fetch_row($result);
//var_dump($row); // the dump proves that the full string comes
// out of the database, with spaces, single and double quotes
$descriptionOfArtifact = $row[0];
$commentsAboutTheDiggingSite = $row[1];
$expertRecommendationsForRestoring = $row[2];
// output the next row from the Artifacts table...
echo <<< NEXT_ROW_HEREDOC
<input type="text" id="Description"
name="descrip" value='$descriptionOfArtifact' readonly="readonly"></input>
<input type="text" id="Comments"
name="comments" value='$commentsAboutTheDiggingSite' readonly="readonly"></input>
<input type="text" id="Experts"
name="experts" value='$expertRecommendationsForRestoring' readonly="readonly"></input>
NEXT_ROW_HEREDOC;
我需要这个表单来显示从数据库中读取并存储在上述PHP变量中的全文字符串。我原以为变量名周围的单引号可以做到这一点,但我认为heredoc(不知何故)搞砸了。
因此,在heredoc中,我如何使用PHP变量并能够看到:
- 单引号
- 双引号
- 空格
- html标记
- 任何可打印的字符,真的
以heredoc的形式?
<<<
和文本末尾关键字之间有一个空格。这可能是问题的原因。
此外,当您将$row[0]
、$row[1]
和$row[2]
分配给变量时,请在它们上调用htmlspecialchars
,并在value="..."
属性上使用双引号。
在简单的Html代码中,您需要用双引号"显示您的值,如果您使用php通过ECHO打印一些Html代码,则需要使用单引号在输入框