QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Home
  • QIdea
  • QTrack
Home Carbon Ads Remove HTML Tags from String using PHP

Remove HTML Tags from String using PHP

In this tutorial I will show you how to remove HTML tags from a string using PHP.

Remove all Tags

PHP’s inbuilt strip_tags function lets you remove all HTML tags from string and also provides you ability to ignore certain tags.

Here is example code on how to use

<?php

$string = "<p>Awesome</p><b> Website</b><i> by Narayan</i>";

echo strip_tags($string, "<b><i>");

?>

Output is

Awesome<b> Website</b><i> by Narayan</i>

Remove all Tags with inner Content

strip_tags doesn’t remove the content inside the removed tag. Here is how to remove the content too

<?php

    function strip_tags_content($text, $tags = '', $invert = FALSE)
    {

        preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags);
        $tags = array_unique($tags[1]);
       
        if(is_array($tags) AND count($tags) > 0)
        {
            if($invert == FALSE)
            {
                return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text);
            }
            else
            {
                return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text);
            }
        }
        elseif($invert == FALSE)
        {
            return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text);
        }
        return $text;
    }

    $string = "<p>Awesome</p><b> Website</b><i> by Narayan</i>. Thanks for visiting";

    echo strip_tags_content($string, "<b><i>");

?>

Output is

<b> Website</b><i> by Narayan</i>. Thanks for visiting

This custom function also let’s you ignore some tags.

Remove Certain Tags

Here is code example on how to remove only certain tags from a string

<?php

    $string = "<p>Awesome</p><b> Website</b><i> by Narayan</i>. Thanks for visiting";
    $tags = array("p", "i");

    foreach($tags as $tag)
    {
        $string = preg_replace("/<\\/?" . $tag . "(.|\\s)*?>/", $replace_with, $string);
    }

    echo $string;
?>

Output is

Awesome<b> Website</b> by Narayan. Thanks for visiting

Remove Certain Tags with Content

Here is code example on how to remove only certain tags from a string with tag content

<?php

    $string = "<p>Awesome</p><b> Website</b><i> by Narayan</i>. Thanks for visiting";
    $tags = array("p", "i");

    echo preg_replace('#<(' . implode( '|', $tags) . ')(?:[^>]+)?>.*?</\1>#s', '', $string);

?>

Output is

<b> Website</b>. Thanks for visiting
Feb 28, 2015Narayan Prusty
Exit-Intent using JavaScriptAdding a Single Image using WordPress Media Uploader
Comments: 1
  1. hari
    7 years ago

    Above code works perfect, excellent coding, thanks for the help.

    Please also guide how can I remove embed tags from the html.

    Input string is : [embed]https://youtu.be/mB-fSxDI0LU[/embed] Run for your lives! It’s the WEENIE-SAURUS REX!

    Great Thanks
    Hari

    ReplyCancel

Leave a Reply to hari Cancel reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Narayan Prusty

I am a software engineer specialising in Blockchain, DevOps and Go/JavaScript. This is my personal blog where I write about things that I learn and feel interesting to share.

8 years ago 2 Comments WordPress
Share this
0
GooglePlus
0
Facebook
0
Twitter
0
Linkedin
  • Remove all Tags
  • Remove all Tags with inner Content
  • Remove Certain Tags
  • Remove Certain Tags with Content
Related Articles
  • Creating Custom WordPress Shortcodes using PHP
  • Javascript Create File Object From URL
  • Add Checkbox using WordPress Settings API
  • WordPress Set HTML Editor as Default
  • WordPress Set Visual Editor as Default
Our Sponsor
My Books

2014 - 2015 © QNimate
All tutorials MIT license