Php echo html decode

PHP html_entity_decode() Function

The HTML output of the code above will be (View Source):

The browser output of the code above will be:

Definition and Usage

The html_entity_decode() function converts HTML entities to characters.

The html_entity_decode() function is the opposite of htmlentities().

Syntax

Parameter Values

Parameter Description
string Required. Specifies the string to decode
flags Optional. Specifies how to handle quotes and which document type to use.

The available quote styles are:

  • ENT_COMPAT — Default. Decodes only double quotes
  • ENT_QUOTES — Decodes double and single quotes
  • ENT_NOQUOTES — Does not decode any quotes

Additional flags for specifying the used doctype:

  • ENT_HTML401 — Default. 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
  • UTF-8 — Default. ASCII compatible multi-byte 8-bit Unicode
  • ISO-8859-1 — Western European
  • ISO-8859-15 — Western European (adds the Euro sign + French and Finnish letters missing in 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
  • Shift_JIS — Japanese
  • EUC-JP — Japanese
  • MacRoman — Character-set that was used by Mac OS

Note: Unrecognized character-sets will be ignored and replaced by ISO-8859-1 in versions prior to PHP 5.4. As of PHP 5.4, it will be ignored an replaced by UTF-8.

Technical Details

Return Value: Returns the converted string
PHP Version: 4.3.0+
Changelog: PHP 5.6 — Changed the default value for the character-set parameter to the value of the default charset (in configuration).
PHP 5.4 — Changed the default value for the character-set parameter to UTF-8.
PHP 5.4 — Added ENT_HTML401, ENT_HTML5, ENT_XML1 and ENT_XHTML.
PHP 5.0 — Added support for multi-byte encodings

More Examples

Example

Convert some HTML entities to characters:

$str = «Albert Einstein said: 'E=MC²'»;
echo html_entity_decode($str, ENT_COMPAT); // Will only convert double quotes
echo «
«;
echo html_entity_decode($str, ENT_QUOTES); // Converts double and single quotes
echo «
«;
echo html_entity_decode($str, ENT_NOQUOTES); // Does not convert any quotes
?>

The HTML output of the code above will be (View Source):

Albert Einstein said: 'E=MC²'

Albert Einstein said: ‘E=MC²’

Albert Einstein said: 'E=MC²'

The browser output of the code above will be:

Example

Convert some HTML entities to characters, using the Western European character-set:

$str = «My name is Øyvind Åsane. I'm Norwegian.»;
echo html_entity_decode($str, ENT_QUOTES, «UTF-8»);
?>

The HTML output of the code above will be (View Source):

The browser output of the code above will be:

Источник

Php echo(html_entity_decode($user));

This tutorials show you how to use html_entity_decode.

html_entity_decode is used in the following way.

echo(html_entity_decode($user)); More precisely, this function decodes all the entities (including all numeric entities) that a) are necessarily valid for the chosen document type ' i.e., for XML, this function does not decode named entities that might be defined in some DTD ' and b) whose character or characters are in the coded character set associated with the chosen encoding and are permitted in the chosen document type. All other entities are left as is.

The html_entity_decode is declared as follows:

html_entity_decode( string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null): string 

The return value is Returns the decoded string.

Examples

// w w w . d em o 2 s . c o m /* if ($_FILES["file"]["error"] > 0) < echo "Error: " . $_FILES["file"]["error"] . "
"; > else < echo "Upload: " . $_FILES["file"]["name"] . "
"; echo "Type: " . $_FILES["file"]["type"] . "
"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; > */
//d?but analyse du fichier // Read the file into an array $myfile=$_FILES["file"]["tmp_name"]; // Read the file into an array $users = file($myfile); foreach ($users as $user) < /* list($name)=explode(";",$line); print($name."\n");*/ echo(html_entity_decode($user)); > //echo var_dump($_FILES); ?>

demo2s.com | Email: | Demo Source and Support. All rights reserved.

Источник

Convert HTML Entities and Special Characters

Learn how to encode or decode all or special HTML characters within a string.

htmlspecialchars()

  1. $string – The input string.
  2. $flags – A bitmask of one or more flags.
  3. $encoding – The default is UTF-8 character set. To use a different character set, pass the character set, for example, BIG5 .
  4. $double_encode – If false PHP will not encode existing HTML entities in the string, the default is true to convert everything.

If you want to display HTML coding on a web page, you should convert the HTML special characters to HTML entities. The htmlspecialchars() function converts the following characters to their HTML entities. This results in the characters being displayed exactly as entered, rather than parsed and rendered by the browser as if they were actual HTML.

  • & (ampersand) converts to &
  • ‘ (single quote) converts to '
  • » (double quote) converts to "
  • < (less than) converts to <
  • > (greater than) converts to >

See the following example:

Note: Use the ENT_QUOTES flag to escape both single and double quotes into HTML entities.

htmlspecialchars_decode()

 echo htmlspecialchars_decode (' " '); // " echo htmlspecialchars_decode (' ' '); // ' echo htmlspecialchars_decode (' ' ', ENT_QUOTES); // ';

htmlentities()

  1. $string – The input string.
  2. $flags – A bitmask of one or more flags.
  3. $encoding – The default is UTF-8 character set. To use a different character set, pass the character set, for example, BIG5 .
  4. $double_encode – If false PHP will not encode existing HTML entities in the string, the default is true to convert everything.

This htmlentities() function is helpful if you need to convert every character with a special meaning in HTML coding. For example, the copyright symbol © , the cent sign ¢ , or the grave accent è . See the following example:

html_entity_decode()

Using this function you can reverse the effect of the htmlentities() function. For example, the © coverts to copyright symbol © , the ¢ converts to cent sign ¢ , or the è coverts to grave accent è . See the following example:

 echo html_entity_decode (' " '); // " echo html_entity_decode (' ' '); // ' echo html_entity_decode (' ' ', ENT_QUOTES); // ';

Flags

The above functions use a bitmask of one or more flags, which specify how to handle quotes, invalid code unit sequences, and the used document type. The default is ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 .

  • ENT_COMPAT – Converts only double-quotes.
  • ENT_QUOTES – Converts both single and double quotes.
  • ENT_NOQUOTES – Doesn’t convert either single or double quotes.
  • ENT_IGNORE – Doesn’t convert anything.
  • ENT_SUBSTITUTE – Replaces invalid code with Unicode replacement characters instead of returning an empty string.
  • ENT_DISALLOWED – Replaces invalid code with Unicode replacement characters instead of leaving them as is.
  • ENT_HTML401 – Handles the code as HTML version 4.01.
  • ENT_XML1 – Handles the code as XML version 1.
  • ENT_XHTML – Handles the code as XHTML.
  • ENT_HTML5 – Handles the code as HTML5.

Double encoding

By default, htmlspecialchars() , htmlspecialchars_decode() , htmlentities() , and html_entity_decode() functions double encode existing character entities. As a result, & is converted to &amp; and " is converted to &quot; . You can use the double_encode named argument to turn off this default behavior, see the following example:

Working with Strings:

Источник

html_entity_decode

html_entity_decode() is the opposite of htmlentities() in that it converts HTML entities in the string to their corresponding characters.

More precisely, this function decodes all the entities (including all numeric entities) that a) are necessarily valid for the chosen document type — i.e., for XML, this function does not decode named entities that might be defined in some DTD — and b) whose character or characters are in the coded character set associated with the chosen encoding and are permitted in the chosen document type. All other entities are left as is.

Parameters

A bitmask of one or more of the following flags, which specify how to handle quotes and which document type to use. The default is ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 .

Available flags constants
Constant Name Description
ENT_COMPAT Will convert double-quotes and leave single-quotes alone.
ENT_QUOTES Will convert both double and single quotes.
ENT_NOQUOTES Will leave both double and single quotes unconverted.
ENT_SUBSTITUTE Replace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or � (otherwise) instead of returning an empty string.
ENT_HTML401 Handle code as HTML 4.01.
ENT_XML1 Handle code as XML 1.
ENT_XHTML Handle code as XHTML.
ENT_HTML5 Handle code as HTML 5.

An optional argument defining the encoding used when converting characters.

If omitted, encoding defaults to the value of the default_charset configuration option.

Although this argument is technically optional, you are highly encouraged to specify the correct value for your code if the default_charset configuration option may be set incorrectly for the given input.

The following character sets are supported:

Supported charsets
Charset Aliases Description
ISO-8859-1 ISO8859-1 Western European, Latin-1.
ISO-8859-5 ISO8859-5 Little used cyrillic charset (Latin/Cyrillic).
ISO-8859-15 ISO8859-15 Western European, Latin-9. Adds the Euro sign, French and Finnish letters missing in Latin-1 (ISO-8859-1).
UTF-8 ASCII compatible multi-byte 8-bit Unicode.
cp866 ibm866, 866 DOS-specific Cyrillic charset.
cp1251 Windows-1251, win-1251, 1251 Windows-specific Cyrillic charset.
cp1252 Windows-1252, 1252 Windows specific charset for Western European.
KOI8-R koi8-ru, koi8r Russian.
BIG5 950 Traditional Chinese, mainly used in Taiwan.
GB2312 936 Simplified Chinese, national standard character set.
BIG5-HKSCS Big5 with Hong Kong extensions, Traditional Chinese.
Shift_JIS SJIS, SJIS-win, cp932, 932 Japanese
EUC-JP EUCJP, eucJP-win Japanese
MacRoman Charset that was used by Mac OS.
» An empty string activates detection from script encoding (Zend multibyte), default_charset and current locale (see nl_langinfo() and setlocale() ), in this order. Not recommended.

Note: Any other character sets are not recognized. The default encoding will be used instead and a warning will be emitted.

Return Values

Returns the decoded string.

Источник

htmlspecialchars_decode

This function is the opposite of htmlspecialchars() . It converts special HTML entities back to characters.

The converted entities are: & , " (when ENT_NOQUOTES is not set), ' (when ENT_QUOTES is set), < and > .

Parameters

A bitmask of one or more of the following flags, which specify how to handle quotes and which document type to use. The default is ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 .

Available flags constants
Constant Name Description
ENT_COMPAT Will convert double-quotes and leave single-quotes alone.
ENT_QUOTES Will convert both double and single quotes.
ENT_NOQUOTES Will leave both double and single quotes unconverted.
ENT_SUBSTITUTE Replace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or � (otherwise) instead of returning an empty string.
ENT_HTML401 Handle code as HTML 4.01.
ENT_XML1 Handle code as XML 1.
ENT_XHTML Handle code as XHTML.
ENT_HTML5 Handle code as HTML 5.

Return Values

Returns the decoded string.

Changelog

Version Description
8.1.0 flags changed from ENT_COMPAT to ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 .

Источник

Читайте также:  Welcome To Pakainfo.com
Оцените статью