Php print html as string

Consider strip_tags[] .

strip_tags["TEXT"];

Output:

TEXT

But what if i want to nullify the effect of the tags but display them as well?

Output:

TEXT

Would i have to use preg_replace[] ? Or is there a more elegant solution available?

Thanks :D

asked May 5, 2010 at 19:20

You can HTML encode the string via htmlspecialchars:

htmlspecialchars["TEXT"];

answered May 5, 2010 at 19:23

D'Arcy RittichD'Arcy Rittich

163k38 gold badges283 silver badges279 bronze badges

You can easily convert characters to their HTML entity using htmlspecialchars or htmlentities. Make sure you check the PHP manual to determine what is most appropriate to your data, as both functions operate slightly differently.

You can then reverse the encoding with htmlspecialchars_decode and html_entity_decode - again, check which is most appropriate for your data.

answered May 5, 2010 at 19:24

akamikeakamike

2,11813 silver badges16 bronze badges

2

You could always just replace with >, yeah?

answered May 5, 2010 at 19:21

ChrisChris

27.2k24 gold badges123 silver badges221 bronze badges

2

You can use textarea tag as following:


answered May 11, 2018 at 7:41

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    HTML tags begin with the less-than character and end with greater-than character, the text inside the tag formatted and presented according to the tag used. Every tag has special meaning to the browser but there are cases when to show plain HTML code in webpage.
    There are various methods in PHP to show the HTML tags as plain text, some of them are discussed below:
    Method 1: Using htmlspecialchars[] function: The htmlspecialchars[] function is an inbuilt function in PHP which is used to convert all predefined characters to HTML entities.

    Syntax:

    string htmlspecialchars[ $string, $flags, $encoding, $double_encode ]
    • $string: This parameter is used to hold the input string.
    • $flags: This parameter is used to hold the flags. It is combination of one or two flags, which tells how to handle quotes.
    • $encoding: It is an optional argument which specifies the encoding which is used when characters are converted. If encoding is not given then it is converted according to PHP default version.
    • $double_encode: If double_encode is turned off then PHP will not encode existing HTML entities. The default is to convert everything.

    Return Values: This function returns the converted string. If there is invalid input string then empty string will returned.

    Example:

    Output:

    Method 2: Using htmlentities[] function: The htmlentities[] function is an inbuilt function in PHP which is used to transform all characters which are applicable to HTML entities. This function converts all characters that are applicable to HTML entity.

    Syntax:

    string htmlentities[ $string, $flags, $encoding, $double_encode ]

    Parameters: This function accepts four parameters as mentioned above and described below:

    • $string: This parameter is used to hold the input string.
    • $flags: This parameter is used to hold the flags. It is combination of one or two flags, which tells how to handle quotes.
    • encoding: It is an optional argument which specifies the encoding which is used when characters are converted. If encoding is not given then it is converted according to PHP default version.
    • $double_encode: If double_encode is turned off then PHP will not encode existing HTML entities. The default is to convert everything.

    Return Values: This function returns the string which has been encoded.

    Example:

    Output:

    Method 3: This method is used to replace the character by set of characters to get the desired output. In this method, < is replaced by < and > is replaced by >.

    Example:

    Output:


    How do I display HTML tags as plain text in PHP?

    If you want to use PHP to show HTML tags as text, then, you can use the function htmlspecialchars[] to escape < and > characters.

    How do I display HTML as plain text?

    HTML Tag The tag tells the browser, that its content must be displayed as an ordinary text without formatting.

    How do I display a PHP string in HTML?

    Use the below code if u get the username from form. $username= htmlspecialchars[$_REQUEST['username']]; or Use the below code if u assign the variable. Show activity on this post.

    Can I write HTML code in PHP?

    While HTML and PHP are two separate programming languages, you might want to use both of them on the same page to take advantage of what they both offer. With one or both of these methods, you can easily embed HTML code in your PHP pages to format them better and make them more user-friendly.

    Chủ Đề