使用HTML内部变量的双引号转义[重复]

时间:2022-09-15 16:26:12

This question already has an answer here:

这个问题在这里已有答案:

For a variable inside a echo that contains HTML, where would I add slashes to escape the double quotes?

对于包含HTML的echo中的变量,我在哪里添加斜杠来转义双引号?

Example:

例:

echo "<input type=\"hidden\" name=\"id\" value=".$row['id']." />";

This part:

这部分:

value=".$row['id']."

3 个解决方案

#1


34  

Some tips on outputting HTML with PHP:

有关使用PHP输出HTML的一些提示:

  1. Use single quotes so that you don't have to escape the double quotes (when using echo),
  2. 使用单引号,这样您就不必转义双引号(使用echo时),
  3. Use htmlspecialchars() to properly escape any "rogue" values you may have.
  4. 使用htmlspecialchars()来正确地转义您可能拥有的任何“流氓”值。

Example using echo:

使用echo的示例:

echo '<input type="hidden" name="id" value="', htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8'), '" />';

Or printf():

或者printf():

printf('<input type="hidden" name="id" value="%s" />', 
    htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8')
);

Or, in HTML mode:

或者,在HTML模式下:

?>
<input type="hidden" name="id" value="<?php echo htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8'); ?>" />
<?php

#2


1  

Use htmlentities:

使用htmlentities:

echo "<input type=\"hidden\" name=\"id\" value=\"".htmlentities($row['id'])."\" />";

#3


0  

How about use single quotes so you don't have to escape any quotes. Like so:

如何使用单引号,这样您就不必转义任何引号。像这样:

echo '<input type="hidden" name="id" value="'.$row['id'].'" />';

#1


34  

Some tips on outputting HTML with PHP:

有关使用PHP输出HTML的一些提示:

  1. Use single quotes so that you don't have to escape the double quotes (when using echo),
  2. 使用单引号,这样您就不必转义双引号(使用echo时),
  3. Use htmlspecialchars() to properly escape any "rogue" values you may have.
  4. 使用htmlspecialchars()来正确地转义您可能拥有的任何“流氓”值。

Example using echo:

使用echo的示例:

echo '<input type="hidden" name="id" value="', htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8'), '" />';

Or printf():

或者printf():

printf('<input type="hidden" name="id" value="%s" />', 
    htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8')
);

Or, in HTML mode:

或者,在HTML模式下:

?>
<input type="hidden" name="id" value="<?php echo htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8'); ?>" />
<?php

#2


1  

Use htmlentities:

使用htmlentities:

echo "<input type=\"hidden\" name=\"id\" value=\"".htmlentities($row['id'])."\" />";

#3


0  

How about use single quotes so you don't have to escape any quotes. Like so:

如何使用单引号,这样您就不必转义任何引号。像这样:

echo '<input type="hidden" name="id" value="'.$row['id'].'" />';