All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Remove HTML Tags From String In PHP

Last Updated : Mar 11, 2024

How To Remove HTML Tags From String In PHP

In this tutorial we will show you the solution of how to remove HTML tags from string in PHP, there are many methods with help of which you are able to remove html tags.

Let us understand them as, mostly used number of methods here are two. First one is using strip tags and other is using remove all.

Step By Step Guide On How To Remove HTML Tags From String In PHP :-

In this article, we will understand first one that is strip tag here. remove all will be discussed in our next section. Let us how to use strip tags with help of codes.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>convert object to array using JavaScript</title>
</head>
<body>
<h1>
 Talkers code
</h1>
<?php
echo strip_tags("Hello to <b><i>Talkers code!</i></b>");
?>
<?php
    $text = "<p><strong>Hello World!</strong></p>";
    $b = strip_tags($text, "<strong><i>");
?>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, inside body we just create a heading and use php codes to show how to use strip_tags().
  6. Now, in first case we directly use echo to split_tags() and inside these tags you see that we use bold and italic tags to some text. But we you see the output, you see that the text is printed or say displayed as it is. No, any tag is applied to Talkers code text.
  7. In next case, in which we use hello world. We store them in a variable that is text and apply strip_tags on this.
  8. Here, you will see the text appeared is weighted text that like strong tag is again applied on it after using strip_tags().
  9. This is because we allow some tags inside strip_tags. And that are strong and italic. But strong and paragraph are applied to text. So, strong tag remained same whereas paragraph tag gets removed because we allows strong and italic tags to format string.
  10. Hence, it seems to be very meaningful where we have to get input from user and have to save it in database. We hope that you understand code properly.
  11. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to remove html tags from string with help of php.

I hope this tutorial on how to remove HTML tags from string in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪