简体   繁体   中英

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. How can I make the s show?

<?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. You can find special characters related codes here https://www.utexas.edu/learn/html/spchar.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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