PHP - Remove HTML Tag from String

Submitted by: 
Visitors have accessed this post 268 times.

In this tutorial we will create a Remove HTML Tag from String using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Before we get started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server

Lastly, this is the link for the bootstrap that i used for the layout design

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as index.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale-1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Remove HTML Tag from String</h3>
  16. <hr style="border=top:1px dotted #ccc;"/>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6">
  19. <?php include 'remove_html.php'?>
  20. </div>
  21. </div>
  22. </body>
  23. </html>

Creating the Main Function

This code contains the main function of the application. This code can remove the html tags inside the html variable.To do this just copy and write the code inside the text editor, then save it as remove_html.php.

  1. <?php
  2. $data = "<h2><strong>Lorem ipsum</strong></h2>";
  3. $data .= "<br />";
  4. $data .= "<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.</p>";
  5. $html = htmlspecialchars($data);
  6. echo $html;
  7. ?>
  8. <br /><br />
  9. <form method="POST" action="">
  10. <center><button class="btn btn-warning" name="remove"><span class="glyphicon glyphicon-remove"></span> Remove Tags</button></center>
  11. </form>
  12. <h3>Result</h3>
  13. <?php
  14. if(ISSET($_POST['remove'])){
  15. $result = htmlspecialchars_decode($html);
  16. $remove = preg_replace('/<[^<]+?>/', ' ', $result);
  17. echo $remove;
  18. }
  19. ?>

There you have it we successfully created Remove HTML Tag from String using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.