Open In App

htmlentities() vs htmlspecialchars() Function in PHP

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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.

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.geeksforgeeks.org">GeeksforGeeks</a>';
     
    // It will convert htmlentities and print them
    echo htmlentities($str);
 
?>


Output:

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

htmlspecialchars() Function

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

Syntax

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

Parameter 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 converted string. If there is an invalid input string then an empty string will be returned. 

Example: This example uses the htmlspecialchars() function to convert all predefined characters to HTML entities. 

PHP




<?php
     
    // String to be converted
    $str = '"geeksforgeeks.org" Go to GeeksforGeeks';
     
    // Converts double and single quotes
    echo htmlspecialchars($str, ENT_QUOTES);
 
?>


Output:

&quot;geeksforgeeks.org&quot; Go to GeeksforGeeks

Difference between htmlentities() and htmlspecialchars() function:

Difference htmlentities() htmlspecialchars()
Purpose Converts all applicable characters to HTML entities. Converts special characters to HTML entities.
Character Encoding Handles a broader set of characters and supports various character encodings, as it takes an optional encoding parameter. Primarily designed for ISO-8859-1 encoding but can work with other encodings if specified.
Ampersand Handling Optionally encodes ampersands (&) even when not part of an entity. Does not encode ampersands (&) unless they are part of a special character sequence (e.g., &lt;, &gt;).
Quote Handling Optionally encodes both single and double quotes. Optionally encodes double quotes (") only.
Use Case Use when dealing with a broader range of characters and when specific character encoding needs are present. Generally used when working with HTML in a Latin1 (ISO-8859-1) context and when only certain characters need encoding.


Last Updated : 29 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads