PHP string doesn't allow < and> characters

后端 未结 4 1592
醉话见心
醉话见心 2020-12-01 20:12

I have a string in my code as per the following example:

\';
?>

相关标签:
4条回答
  • 2020-12-01 20:32

    you can use the htmlspecialchars function to escape the < brackets..

    0 讨论(0)
  • 2020-12-01 20:39

    Use htmlspecialchars()

    echo htmlspecialchars('string you want to echo');
    
    0 讨论(0)
  • 2020-12-01 20:40

    With PHP you can generate HTML markup, so you have to find a way to distinguish between HTML element characters ( < & > ). There exist special sequence of characters in HTML that are called HTML entities. Those are described with an ampersand, some sort of shorthand and end with a semi-colon.

    Here are some examples:

    &gt;     : > (greater-than)
    &lt;     : < (less-than)
    &amp;    : & (ampersand)
    &raquo;  : » (right angle quote marks)
    &eacute; : é (e acute)
    

    Almost all characters can be represented with such entities, but it quickly gets tedious. You only have to remember the < and > ones, plus the & for URLs.

    Your code should be rewritten like this if your intention was to show the less-than / greater-than signs.

    <?php
    $find = '&lt;tag';
    $string = 'blah blah &lt;tag=something&gt;';
    ?>
    

    As mentioned in other answers, you can use the function htmlspecialchars() to convert characters in a variable (e.g. from user input).

    <?php echo htmlspecialchars($string); ?>
    

    will display blah blah <tag=something> for viewing in a web browser. Else, if you were using PHP on the command line for example, you would not need to use this function.

    0 讨论(0)
  • 2020-12-01 20:42

    You need to use HTML characters to avoid it being turned into HTML.

    so:

    echo htmlspecialchars("<hello>");
    
    0 讨论(0)
提交回复
热议问题