如何显示带有撇号的文本框的值


PHP: How to display value to textbox that has an apostrophe?

我在显示具有撇号的文本框的值时遇到问题。在下面的代码中,结果将只显示没有s的sample。我如何使s显示?

<?php
$str = "sample's";
echo "<input type='text' value='".$str."' />";
?>

使用htmlspecialchars()ENT_QUOTES

echo "<input type='text' value='".htmlspecialchars($str,ENT_QUOTES)."' />";

可以使用addslashes函数

$str = addslashes("sample's");

在回显时使用

echo stripslashes($str);

你可以试试:

echo '<input type="text" name="name" value="'.$str.'" />';

echo "<input type='text' name='name' value='"{$str}'" />";

试试这个

$str = "sample's";
echo "<input type='text' value='".str_replace("'", '&#39;', $str)."' />";

注意我在这里使用了ISO Latin-1代码的HTML特殊字符。您可以在这里找到特殊字符相关代码https://www.utexas.edu/learn/html/spchar.html