Remove HTML Tags from String using PHP

Remove HTML Tags from String using PHP

In this post we will show you how to remove HTML tags from a string using PHP different method and functions.

Remove all Tags from string

use strip_tags remove all html tag from string

$html_string = '

Test onlinecode

Other text test
'; echo strip_tags($html_string);

output

Test onlinecode Other text test

php function strip_tags wil remove all HTML tags from string and also provides you ability to ignore certain tags. by using thi metho we can remove all ignore some specific tag in php sting.

Here is example code on how to use strip_tags with ignore some specific tag

$html_string = "

Testing string of

Website onlinecode"; echo strip_tags($html_string, "");

Output is


Testing string of  Website onlinecode

Remove all Tags with inner Content in php sting.

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

function strip_tags_remove($text, $html_tags = '', $invert = FALSE)
{

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

$html_string = "

Testing string of

Website onlinecode. Thanks for visiting"; echo strip_tags_remove($html_string, "");

Output is


 Website onlinecode. Thanks for visiting

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

Remove Certain Tags in php sting.

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

$html_string = "

Testing string of

Website onlinecode. Thanks for visiting"; $html_tags = array("p", "i"); foreach($html_tags as $tag_val) { $html_string = preg_replace("/<\\/?" . $tag_val . "(.|\\s)*?>/", $replace_with, $html_string); } echo $html_string;

Output is


Testing string of  Website onlinecode. Thanks for visiting

Remove Certain Tags with Content in php sting.

Here is code example on Remove HTML Tags from String using PHP

$html_string = "

Testing string of

Website onlinecode. Thanks for visiting"; $html_tags = array("p", "i"); echo preg_replace('#<(' . implode( '|', $html_tags) . ')(?:[^>]+)?>.*?#s', '', $html_string);

Output is


 Website. Thanks for visiting

Remove HTML Tags Style from String in PHP

this following code will remove only “style” fomr HTML Tags Style from String in PHP.

$html_string  = '

This is test for remove style

Remove HTML Tags Style from String IN onlinecode

'; $html_output = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $html_string ); echo $html_output;

Output is


This is test for remove style. Remove HTML Tags Style from String IN onlinecode

Leave a Comment

Your email address will not be published. Required fields are marked *

4  +  1  =  

We accept paid advance and paid guest Posting on our Site : Contact US