Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php - print html tags as text

Tags:

html

php

tags

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 ?

like image 962
Obmerk Kronen Avatar asked Nov 28 '25 02:11

Obmerk Kronen


1 Answers

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.

like image 135
Niet the Dark Absol Avatar answered Nov 30 '25 17:11

Niet the Dark Absol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!