PHP htmlspecialchars() Function
You are Here:
PHP htmlspecialchars()
The htmlspecialchars()
function converts special characters to HTML entities.
The pre-defined characters are:
- & (ampersand) becomes &
- " (double quote) becomes "
- ' (single quote) becomes '
- < (less than) becomes <
- > (greater than) becomes >
Tips: If you require all input substrings that have associated named entities to be translated, use htmlentities() instead.
Example
PHP Compiler
<?php
$str = "This is <u>underline</u>.";
echo htmlspecialchars($str) . "<br>";
echo ($str);
?>
Output
This is <u>underline</u>.
This is underline.
Syntax
htmlspecialchars(str, flag, character-set, double_encode)
Parameter Values
Value | Type | Explanation |
---|---|---|
str | Required | Specifies the input string. |
flag | Optional | Specifies how to handle quotes, invalid code unit sequences and the used document type. Possible Values:
|
character-set | Optional | Specifies the character-set to be used when converting characters. Possible Values:
|
double_encode | Optional | Specifies whether to encode existing html entities or not.
|
Return Value
Value | Explanation |
---|---|
String | Returns the converted string. |
Reminder
Hi Developers, we almost covered 90% of String functions and Interview Question on PHP with examples for quick and easy learning.
We are working to cover every Single Concept in PHP.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.