htmlentities() Function

The htmlentities() function is an inbuilt function in PHP that is used to transform all characters that apply to HTML entities. It is used when additional character encoding is required.

Syntax:

string htmlentities( $string, $flags, $encoding, $double_encode )

Parameters value:

Parameter Description
$string It is used to hold the input string.
$flags It is used to hold the flags. It is a combination of one or two flags, which tells how to handle quotes.
$encoding Optional argument specifying the encoding used when characters are converted. Defaults to PHP default.
$double_encode If double_encode is turned off, PHP will not encode existing HTML entities. Default is to convert everything.

Return Values

This function returns the string which has been encoded. 

Example: This example uses the htmlentities() function to transform all characters which are applicable to HTML entities.

PHP




<?php
     
    // String convertible to htmlentities
    $str = '<a href="https://www.w3wiki.org">w3wiki</a>';
     
    // It will convert htmlentities and print them
    echo htmlentities($str);
 
?>


Output:

&lt;a href=&quot;https://www.w3wiki.org&quot;&gt;w3wiki&lt;/a&gt;

htmlentities() vs htmlspecialchars() Function in PHP

The htmlentities( ) and htmlspecialchars( ) in PHP both convert special characters to their HTML entities, but ‘htmlspecialchars()’ only converts characters that have special meaning in HTML, while ‘htmlentities( )’ converts a broader range of characters. In this article, we will see what htmlentities( ) & htmlspecialchars( ) Function is used for & also understand their implementation through the examples.

Table of Content

  • htmlentities() Function
  • htmlspecialchars() Function

Similar Reads

htmlentities() Function

The htmlentities() function is an inbuilt function in PHP that is used to transform all characters that apply to HTML entities. It is used when additional character encoding is required....

htmlspecialchars() Function

...

Difference between htmlentities() and htmlspecialchars() function:

The htmlspecialchars() function is an inbuilt function in PHP which is used to convert all predefined characters to HTML entities....