Print less-than and greater-than symbols in PHP

后端 未结 7 719
南方客
南方客 2020-12-01 16:01

I am have troubles trying to print out < > symbols in HTML using PHP.

I am appending a string \"\" to a v

相关标签:
7条回答
  • 2020-12-01 16:42

    You need to turn them into e.g. &lt; and &gt; - see the htmlentities() or htmlspecialchars() functions.

    0 讨论(0)
  • 2020-12-01 16:42
    echo htmlentities($output);
    

    or

    echo htmlspecialchars($output);
    

    If you don't want to bother manually going through your string and replacing the entities.

    0 讨论(0)
  • 2020-12-01 16:48

    If you are outputting HTML, you cannot just use < and > : you must use the corresponding HTML entities : &lt; and &gt;


    If you have a string in PHP and want to automatically replace those characters by the corresponding HTML entities, you'll be interested by the htmlspecialchars() function (quoting) :

    The translations performed are:

    • '&' (ampersand) becomes '&amp;'
    • '"' (double quote) becomes '&quot;' when ENT_NOQUOTES is not set.
    • "'" (single quote) becomes '&#039;' only when ENT_QUOTES is set.
    • '<' (less than) becomes '&lt;'
    • '>' (greater than) becomes '&gt;'


    In your case, a portion of code like this one :

    $output = " ";

    echo htmlspecialchars($output, ENT_COMPAT, 'UTF-8');
    

    Would get you the following HTML code as output :

     &lt;machine&gt; 
    


    And, just in case, if you want to encode more characters, you should take a look at the htmlentities() function.

    0 讨论(0)
  • 2020-12-01 16:50

    &gt; = >
    &lt; = <

    Or you can use htmlspecialchars.

    $output .= htmlspecialchars(" <machine> ");
    
    0 讨论(0)
  • 2020-12-01 17:01

    Your trouble is not with PHP, but rather with the fact that < and > are used in HTML. If you want them to display in the browser, you probably want to print out their escaped entity versions:

    • < is &lt;
    • > is &gt;

    You can also use the htmlspecialchars() function to automatically convert them:

    echo htmlspecialchars("<machine>");
    
    0 讨论(0)
  • 2020-12-01 17:02

    use "htmlspecialchars_decode()"

    e.g.

    <?php 
    $a= htmlspecialchars('<?php ');
    $a=$a.htmlspecialchars('echo shell_exec("ipconfig"); ');
    $a=$a.htmlspecialchars('?>');
    
    echo htmlspecialchars_decode($a);
    ?> 
    
    0 讨论(0)
提交回复
热议问题