Change language

Htmlentities () vs htmlspecialchars () function in PHP

Function htmlentities() - it is a built-in function in PHP that is used to convert all characters applicable to HTML objects. This function converts all characters that are applicable to the HTML entity.Syntax
string htmlentities ($string, $flags, $encoding, $double_encode)
Parameters:This function takes four parameters as above and described below:
  • $string :this parameter is used to store the input string.
  • $flags:this parameter is used to store flags. This is a combination of one or two flags that tells how to handle quotes.
  • $encoding:This is an optional argument that specifies the encoding to use when converting characters. If no encoding is specified, it will be converted according to the default PHP version.
  • $double_encode:if double_encode is disabled, then PHP will not encode existing HTML entities. By default, everything is converted.
Return Values:This function returns the string that was encoded.Example:   // String converted to htmlentities $str = GeeksforGeeks ;  
// It converts htmlentities and prints them echo htmlentities ( $str ); ?>
Exit:
& amp; lt ; a href = & amp; quot; https://www.engineerforengineer.org& quot; & amp; gt; GeeksforGeeks & amp; lt; / a & amp; gt;
htmlspecialchars() function htmlspecialchars() function - it is a built-in function in PHP that is used to convert all predefined characters to HTML entities.Syntax:
string htmlspecialchars ( $string, $flags, $encoding, $double_encode)
  • $string:this parameter is used to store the input string.
  • $flags:this parameter is used to store flags. This is a combination of one or two flags that tells how to handle quotes.
  • $encoding:This is an optional argument that specifies the encoding to use when converting characters. If no encoding is specified, it will be converted according to the default PHP version.
  • $double_encode:if double_encode is disabled, then PHP will not encode existing HTML entities. By default, everything is converted.
Return Values:This function returns a converted string. If the input string is invalid, an empty string is returned.Example :  
// Example htmlspecialchars() function 
// String to convert $str = ’" engineerforengineer.org "Go to GeeksforGeeks’ ;  
// Converts double and single quotes echo htmlspecialchars ( $str , ENT_QUOTES); ?>
Exit:
& amp; quot ; engineerforengineer.org & amp; quot; Go to GeeksforGeeks
Difference between htmlentities() and htmlspecialchars():The only difference between these functions:
  • The htmlspecialchars() function converts special characters to HTML entities.
  • The htmlentities() function converts all applicable characters to HTML entities.

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically