How should I echo a PHP string variable that contains special characters?

前端 未结 4 1208
轻奢々
轻奢々 2021-01-17 09:05

I\'m trying to populate a form with some data that contains special characters (e.g. single quote, double quote,<,>,?,\",\"\".~,,!@#$%^&*()_+}{\":?<<>,./;\'[.]

相关标签:
4条回答
  • 2021-01-17 09:47

    If you want to display it

    echo htmlspecialchars($messge, ENT_QUOTES, 'UTF-8');
    

    That's what I usually do.

    Since the answers are difference:

    htmlentities-vs-htmlspecialchars is worth checking out.

    0 讨论(0)
  • 2021-01-17 09:47

    whats wrong with using a constant ?

    <?php
    define(foo,'<,>,?,","".~,,!@#$%^&*()_+}{":?<<>,./;');
    $foo2="'[.]";
    echo constant('foo').$foo2;
    ?>
    

    you need to put the '[.]' into a variable, as a constant will break on a ' (single quote).

    0 讨论(0)
  • 2021-01-17 09:49

    I normally use the following code, see htmlspecialchars

    <?php echo htmlspecialchars($videoId, ENT_QUOTES | ENT_HTML5); ?>
    
    0 讨论(0)
  • 2021-01-17 09:53

    This will prevent your tags from being broken by the echo:

    <?php echo htmlentities($message); ?>

    0 讨论(0)
提交回复
热议问题