简体   繁体   English

PHP:如何在带有撇号的文本框中显示值?

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

I'm having a problem displaying the value of textbox that has an apostrophe. 我在显示带有撇号的文本框的值时遇到问题。 In my code below, the result will only show sample without an s. 在下面的代码中,结果将仅显示不带s的sample How can I make the s show? 我怎样才能让s表演?

<?php
$str = "sample's";

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

You can use addslashes function 您可以使用添加斜线功能

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

While echoing use 在回显使用时

echo stripslashes($str);

htmlspecialchars()ENT_QUOTES

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

You could try either: 您可以尝试以下任一方法:

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

or 要么

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

Try this 尝试这个

$str = "sample's";

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

Note I have useed ISO Latin-1 code for HTML special character here. 注意我在这里使用ISO Latin-1代码作为HTML特殊字符。 You can find special characters related codes here https://www.utexas.edu/learn/html/spchar.html 您可以在这里找到与特殊字符相关的代码https://www.utexas.edu/learn/html/spchar.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM