How To Remove HTML Tags From String Using PHP – Examples
When using PHP to process the server side requirements of data cleansing, it can be immensely helpful to know how to remove HTML Tags from the user input. If these html tags are not removed, then your application may break, depending upon it’s architecture. So assuming that you do not want user to allow user to input HTML, what would you do if someone does enter HTML in input? In this article, I am going to share a very easy way to use PHP to remove html tags from user input.
Examples on how to remove HTML Tags from String using PHP
In order to see how to remove html from input, let’s assume that we have custom text with HTML tags in it. Now we will see how to remove these html tags using an example.
Example: Remove HTML tags from string using PHP
<?php $content = '<h1>Hi</h1>, How are you?'; echo strip_tags($content); echo '<p>The above will produce the output "Hi, How are you?". The change that happens is that the h1 tags will be removed.</p>'; ?>
To summarize, simply apply the function strip_tags over the content that you want to remove HTML tags from.
Simple, isn’t it?
Do you know of any other ways to use PHP to remove html tags? Feel free to suggest by commenting below.