Php - print html tags as text

12,202

Basically all you really need to make HTML readable as text is:

$out = strtr($input,Array("<"=>"&lt;","&"=>"&amp;"));

htmlspecialchars is basically a subset of htmlentities. htmlentities encodes EVERYTHING that has an entity alternative, namely all the named entities and a handful of codes.

Share:
12,202
Obmerk Kronen
Author by

Obmerk Kronen

Typical GeekGirl lost in a maze of code.

Updated on June 04, 2022

Comments

  • Obmerk Kronen
    Obmerk Kronen almost 2 years

    I need to print an HTML tag on screen as texts (and not in code).

    I need the readers to literally read the tag.

    What is the best practice to do so ?

    print htmlspecialchars('<meta name="copyright" content="© Winston Smith, 1984">');
    

    or

    print htmlentities('<meta name="copyright" content="© Winston Smith, 1984">');
    

    or none of the above.

    • Is there a better way?
    • What else should I consider ?
    • And last question - should I use print, echo or is there no difference ?

    *EDIT I*

    I already seen a problem with the character "©" with htmlentities(). This only confirms my doubts - which one is safe to use for all cases - and is there another way to print html tags as text ?