Hướng dẫn
Quảng cáo

Hàm html_entity_decode() trong PHP

Hướng dẫn cách sử dụng hàm html_entity_decode() trong lập trình PHP

Tác dụng của hàm html-entity-decode()

The html_entity_decode() function converts HTML entities to their corresponding characters.

This function typically reverses the effect of htmlentities() function.

The following table summarizes the technical details of this function.

Return Value: Returns the decoded string.
Version: PHP 4.3.0+

Syntax

The basic syntax of the html_entity_decode() function is given with:

html_entity_decode(string, flags, charset);

The following example shows the html_entity_decode() function in action.

Ví dụ

<?php
// Sample string
$str = "It's an <b>amazing</b> story.";

// Encoding the string
$encoded_str = htmlentities($str);
echo $encoded_str . "<br>";

// Decoding the string
$decoded_str = html_entity_decode($encoded_str);
echo $decoded_str;
?>

 


Parameters

The html_entity_decode() function accepts the following parameters.

Parameter Description
string Required. Specifies the string to decode.
flags

Optional. Specifies how to handle quotes and which document type to use.

The available flags constants for handling quotes are:

  • ENT_COMPAT – Converts double-quotes and leave single-quotes unconverted.
  • ENT_QUOTES – Convert both double and single quotes.
  • ENT_NOQUOTES – Leave both double and single quotes unconverted.

The available flags constants for specifying the document types are:

  • ENT_HTML401 – Handle code as HTML 4.01.
  • ENT_HTML5 – Handle code as HTML 5.
  • ENT_XML1 – Handle code as XML 1.
  • ENT_XHTML – Handle code as XHTML.

The default value for this parameter is ENT_COMPAT | ENT_HTML401.

charset

Optional. Specifies which character set to use. Supported charsets are:

  • UTF-8 – ASCII compatible multi-byte 8-bit Unicode.
  • ISO-8859-1 – Western European, Latin-1.
  • ISO-8859-5 – Little used cyrillic charset (Latin/Cyrillic).
  • ISO-8859-15 Western European, Latin-9. Adds the Euro sign, French and Finnish letters missing in Latin-1 (ISO-8859-1).
  • cp866 – DOS-specific Cyrillic charset.
  • cp1251 – Windows-specific Cyrillic charset.
  • cp1252 – Windows specific charset for Western European.
  • KOI8-R – Russian.
  • BIG5 – Traditional Chinese, mainly used in Taiwan.
  • GB2312 – Simplified Chinese, national standard character set.
  • BIG5-HKSCS – Big5 with Hong Kong extensions, Traditional Chinese.
  • Shift_JIS – Japanese.
  • EUC-JP – Japanese.
  • MacRoman – Charset that was used by Mac OS.

If this parameter is omitted, it defaults to the value of the default_charset configuration option (inside php.ini file).


More Examples

Here're some more examples showing how html_entity_decode() function actually works:

The following example demonstrates the handling of single and double quotes using this function.

Ví dụ

<?php
// Sample string
$str = "I'll \"leave\" tomorrow.";

// Encoding the string
$encoded_str = htmlentities($str, ENT_QUOTES);
echo $encoded_str; /* I'll &quot;leave&quot; tomorrow. */

// Converts only double-quotes
$a = html_entity_decode($encoded_str);
echo $a; /* I'll "leave" tomorrow. */

// Converts both double and single quotes
$b = html_entity_decode($encoded_str, ENT_QUOTES);
echo $b; /* I'll "leave" tomorrow. */
?>

However, in the browser you will always see the string I'll "leave" tomorrow. View source (right-click and select View Page Source) of the example output to see the converted string.

 

Bài viết này đã giúp ích cho bạn?

Bài viết mới

Advertisements