PHP converts a pre-defined HTML entity to a character's function Htmlspecialchars_decode ()

Source: Internet
Author: User

Instance

The pre-defined HTML entity "<" (less than) and ">" (greater than) converted to characters:

<?php$str = "This is some &lt;b&gt;bold&lt;/b&gt; Text. "; echo Htmlspecialchars_decode ($STR);? >

The HTML output of the above code is as follows (see source code):

<! DOCTYPE Html>

The browser output of the above code is as follows:

This is some bold text.

Definition and usage

The Htmlspecialchars_decode () function converts some pre-defined HTML entities to characters.

    • The HTML entities that will be decoded are:

    • &amp; Decode into & (and No.)

    • &quot; Decode into "(double quotes)

    • ' Decode into ' (single quote)

    • &lt; Decode into < (less than)

    • &gt; Decode into > (greater than)

The Htmlspecialchars_decode () function is an inverse function of the htmlspecialchars () function.

Grammar

Htmlspecialchars_decode (String,flags)
Parameters Describe
String Necessary. Specifies the string to decode.
flags optional. Specifies how quotation marks are handled and which document type is used.

Available Quote types:

  • Ent_compat-Default. Decodes only double quotes.

  • Ent_quotes-decodes double and single quotes.

  • Ent_noquotes-do not decode any quotation marks. Additional flags for the document type specified by

:

  • E nt_html401-Default. The code is processed as HTML 4.01.

  • ENT_HTML5-processing code as HTML 5.

  • ENT_XML1-processing code as XML 1.

  • ent_xhtml-as XHTML processing code.

technical details

return value: returns the converted string.
php version: 5.1.0+
update log:

More examples

Example 1

Convert some of the predefined HTML entities to characters:

<?php$str = "Jane &amp;" Tarzan ' "; Echo Htmlspecialchars_decode ($str, Ent_compat);  default, decode double quotes only echo "<br>"; Echo Htmlspecialchars_decode ($str, ent_quotes);//  decode double and single quotes echo "<br>" ; Echo Htmlspecialchars_decode ($str, ent_noquotes); Do not decode any quotes?>

The HTML output of the above code is as follows (see source code):

<! DOCTYPE Html>

The browser output of the above code is as follows:

Jane & ' Tarzan ' Jane & ' Tarzan ' Jane & ' Tarzan '

Example 2

To convert a predefined HTML entity to double quotes:

<?php$str = ' I love &quot; Php&quot;. '; Echo Htmlspecialchars_decode ($str, ent_quotes); Decode double and single quotes?>

The HTML output of the above code is as follows (see source code):

<! DOCTYPE html>

The browser output of the above code is as follows:

I Love "PHP".

The Htmlspecialchars_decode () function converts some pre-defined HTML entities to characters.

<?php$str = "This is some &lt;b&gt;bold&lt;/b&gt; Text. "; echo Htmlspecialchars_decode ($STR);? >

will be parsed into

<! DOCTYPE Html>

The output of the user's product details.

<p>    {sh: $info .intro|htmlspecialchars_decode}</p>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.