简体   繁体   中英

Escape double quotes in input value

I have an input field where the value echoed in PHP is encoded with a UTF-8 charset

<input value="<?php echo html_entity_decode($data["title"]); ?>" type="text">

Sample value:

"पीला सितारा పసుపు నక్షత్రం yellow star!"

The double quotes that are appearing in the text are preventing the value from being displayed.

I have tried this, but it is not working.

<input name="title" id="title" value="<?php echo htmlspecialchars_decode(html_entity_decode($data["title"])); ?>"  class="width9" type="text"> type="text">

The problem is that you do it the wrong way around. You try to decode encoded strings. But you have to encode the string.

So you need

htmlspecialchars($data["title"])

and not

htmlspecialchars_decode($data["title"])

Because htmlspecialchars_decode() decodes a htmlspecialchars encoded string.

htmlspecialchars($string) should work, so should htmlspecialchars($string,ENT_QUOTES,"UTF-8") and htmlentities($value, ENT_COMPAT, "UTF-8")

i wonder if your issue is not actually that you are not specifying the meta charset in your final page

some code for you to test

<?php

if(empty($_POST['string_to_process'])) { $string = "enter your string"; } else { $string = $_POST['string_to_process']; }

?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    </head>
    <body>
        <form action="/encoding.php" method="POST">
            <input type="text" name="string_to_process" />
            <input type="submit" value="submit">
        </form>

        RESULTS:
        WITHOUT ESCAPING : <input type="text" value="<?php echo($string) ?>" /><br />
        FOR htmlspecialchars($string): <input type="text" value="<?php echo(htmlspecialchars($string)) ?>" /><br />
        FOR htmlentities($string): <input type="text" value="<?php echo(htmlentities($string)) ?>" /><br />
        FOR htmlspecialchars($string,ENT_QUOTES,"UTF-8"): <input type="text" value="<?php echo(htmlspecialchars($string,ENT_QUOTES,"UTF-8")) ?>" /><br />
        FOR htmlentities($value, ENT_COMPAT, "UTF-8"): <input type="text" value="<?php echo(htmlentities($string, ENT_COMPAT, "UTF-8")) ?>" /><br />

    </body>
</html>

hope it helps dude, encoding can be a pain

EDIT:

yeah i just tried it without

<meta http-equiv="content-type" content="text/html;charset=utf-8" />

and it was messed up , include the above and all will be peachy

TH

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