<b>Hello this is bold</b> <i>This is italic</i> This is normal
this is a double quote " this is & this is less than < this is greater than >
Now to display the code for the above line formatted in different style we have to use htmlspecialchars() function like this
$contents="<b>Hello this is bold</b> <i>This is italic</i> This is normal this is a double quote " this is & this is less than < this is greater than >";
echo htmlspecialchars($content);
We can display some special chars especially html tags on the screen by using htmlspecialchars function of PHP. This is required when we have to show some sample codes on the page or screen. For example I want to display this line
if ($i < 5 )
Here we can't write < as it is , in this place I have to write < then while displaying this will display < on the screen. Same way some other chars are there which are to be written in different way or special care to be taken for displaying them. All these jobs can be done by using htmlspecialchars function.$input = "<script>alert('XSS');</script>";
$sanitized_input = htmlspecialchars($input);
echo $sanitized_input; // Output: <script>alert('XSS');</script>
$str = 'Hello & welcome!';
echo htmlspecialchars($str); // Output: Hello & welcome!
$str = '© 2023 Plus2Net';
echo htmlspecialchars($str); // Output: © 2023 Plus2Net
echo htmlentities($str); // Output: © 2023 Plus2Net
These examples improve the explanation and show practical uses for securing input and understanding the function's behavior in different cases.
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.
webchecker | 17-02-2010 |
very good explanation, thank you |