In this tutorial we will learn How to Convert Special Characters To HTML Entities in PHP. PHP built-in htmlspecialchars() function is used to convert some special characters to HTML entities.
Table of Contents
Using htmlspecialchars() Function
We can simply use htmlspecialchars() Function to convert special characters to HTML Entities.
htmlspecialchars() Function is a built-in PHP function which converts some predefined characters to HTML entities.
Take a look at the code given below.
<?php
$string = "This is <b>HowToCodeSchool.com</b>.";
echo htmlspecialchars($string);
?>
$string variable contains HTML entities which were converted from special characters.
Output
This is <b>HowToCodeSchool.com</b>
Some Special Characters are:
- &
- "
- '
- <
- >
Their HTML Entities are:
- &
- "
- '
- <
- >