ARTICLE

String Functions in PHP: Part 6

Posted by Sharad Gupta Articles | PHP February 19, 2013
In this article I describe the PHP string functions html entity decode, htmlentities and htmlspecialchars decode.
Reader Level:

Introduction

In this article I describe the PHP string functions html_entity_decode, htmlentities and htmlspecialchars_decode. To learn some other math functions, go to:

  1. String Functions in PHP: Part1
  2. String Functions in PHP:Part2 
  3. String Functions in PHP:Part3
  4. String Functions in PHP:Part4 
  5. String Functions in PHP:Part5

PHP html_entity_decode() Function

The PHP string html_entity_decode function converts all HTML entities to their applicable characters.

Syntax

html_entity_decode(string, quotestyle,character-set)

Parameter in html_entity_decode Function

The parameters of the html_entity_decode function are:

Parameter Description
string It specifies the input string.
quotestyle It specifies how to decode single or double quotes. The possible quote styles are:
  • ENT_COMPAT - Default. Decodes only double quotes.
  • ENT_QUOTES - Decodes double and single quotes.
  • ENT_NOQUOTES - Does not decode any quotes.
  • ENT_HTML401 - Handle code as HTML 4.01.
  • ENT_XML1 - Handle code as XML1. 
character-set It specifies a string that specifies what character set is used.

Example

An example of the
html_entity_decode function is: 

<?php

$str = "We are learning <b>PHP</b> at <a href='http://www.c-sharpcorner.com/1/279/php.aspx'>C-sharpCorner.com</a>";

echo html_entity_decode($str);

?>

Output

html-entity-decode-string-function-in-php.jpg 

PHP htmlentities() Function

The PHP string htmlentities function converts all applicable characters to HTML entities.

Syntax

htmlentities (string, quotestyle,character-set)

Parameter in htmlentities Function

The parameters of the htmlentities  function are:

Parameter Description
string It specifies the string to convert.
quotestyle It specifies how to encode single or double quotes. The possible quote styles are:
  • ENT_COMPAT - Default. Decodes only double quotes.
  • ENT_QUOTES - Decodes double and single quotes.
  • ENT_NOQUOTES - Does not decode any quotes.
  • ENT_HTML401 - Handle code as HTML 4.01.
  • ENT_XML1 - Handle code as XML1.
character-set It specifies a string that specifies what character set is used.

Example

An example of the
htmlentities function is: 

<?php

$str = "MCN Solution at <b>India</b>";

echo "With Out htmlentities Function:". $str."</br>";

echo "When use htmlentities Function:" .htmlentities($str);

?>

Output

htmlentities-string-function-in-PHP.jpg
 

PHP htmlspecialchars_decode() Function

The PHP string htmlspecialchars_decode function converts special HTML entities back to characters.

Syntax

htnlspecialchars_decode (string, quotestyle)

Parameter in htmlspecialchars_decode Function

The parameters of the htmlspecialchars_decode function are:

Parameter Description
string It specifies the string to decode.
quotestyle It specifies how to decode single or double quotes. And quote style are
  • ENT_COMPAT - Default. Decodes only double quotes.
  • ENT_QUOTES - Decodes double and single quotes.
  • ENT_NOQUOTES - Does not decode any quotes.
  • ENT_HTML401 - Handle code as HTML 4.01.
  • ENT_XML1 - Handle code as XML1.

Example

An example of the
htmlspecialchars_decode function is: 

<?php

$input_string = "&#169 C-sharpcorner.com";

echo 'After decoding : '.htmlspecialchars_decode($input_string) .'<br>';

$input_string = "&lt;table&gt;We are learning php&lt;/td&gt;<br>&lt;/tr&gt;<br>&lt;/table&gt;";

echo 'After decoding : '.htmlspecialchars_decode($input_string) .'<br>';

?>
 

Output

htmlspecialchars-decode-string-function-in-php.jpg

Login to add your contents and source code to this article
post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter