PHP Convert Special Characters To HTML Entities
Last Updated : Jul 1, 2023
In this article we will show you the solution of PHP convert special characters to html entities, if you want to learn conversion of special character into html entities then don’t stress yourself just utilize this article.
One more thing is you have to see the clear output after run the given example code on browser right click and open view page source.
It make you see the difference and process of htmlspecialchars_decode(). To achieve the htmlspecialchars_decode() function helps a lot to you by converts characters to HTML entities.
You can also bring back normal flow or form by special method in php later we will see.
Step By Step Guide On PHP Convert Special Characters To Html Entities :-
In this program first we need to create a variable to hold html entity data whether that’s one line or anything.
Here i gave anchor tag with one text with quotes in the form of special characters.
You can define or give input as any html entity as special characters value as your wish.
Simply printing that variable to client side by pass to echo statement provide you html entity result but not actual html output. To do that htmlspecialcharacters_decode() is the perfect method.
<?php $s1="'Hi' <a href='test'>Test</a>"; echo "Special Characters Input: ".$s1."<br><br>"; echo "<br>HTML Entity Output: ".htmlspecialchars_decode($s1); ?>
- A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
- The default file extension for php files is “.php” and php statements end with ‘;’ semicolon. In php we need to specify ‘$’ symbol before the variable name also default rule.
- As for results, we created variable $s, here you can change this variable name as per your wish but keep in mind pre-defined rules for variable definition.
- At the right hand side we passed string of special characters values ‘'Hi' <a href='test'>Test</a>’. Passing variable s1 to echo statement prints result as ‘'Hi' <a href='test'>Test</a>’.
- But do you remember if we write this line in html file what will be our output? Yeah! This is not result, so passing s1 to htmlspecialchars_decode() gives the right output.
- Using right click button, opening source code page also make you understand the process of htmlspecialchars_decode().
- Like said in intro, you can bring back normal form by using htmlspecialcharacters() function in php. We see this function in another tutorial with sample code.
- Instead of echo() you can prefer print() method if you want and that not going to affects your result.
In conclusion, hope now you know how to convert special character to html entity using php.
When you work with php we need to create and process php files at server location and then we need to start the server before execute the program.
Otherwise sometimes you will get error so ensure it if you get any error while running output.
When we execute this program on browser it will show result as ‘Special Characters Input: 'Hi' <a href='test'>Test</a> HTML Entity Output: 'Hi' Test’ output.
With this help of sample code you can test any array of special characters.
I hope this article on PHP convert special characters to html entities helps you and the steps and method mentioned above are easy to follow and implement.