Print less-than and greater-than symbols in PHP

后端 未结 7 748
南方客
南方客 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:48

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


    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 '&'
    • '"' (double quote) becomes '"' when ENT_NOQUOTES is not set.
    • "'" (single quote) becomes ''' only when ENT_QUOTES is set.
    • '<' (less than) becomes '<'
    • '>' (greater than) becomes '>'


    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 :

     <machine> 
    


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

提交回复
热议问题