remove all html tags from string php – 4 Ways to Remove HTML Tags From a String?

remove all html tags from string php: To remove all HTML tags from a string, use the strip_tags function. PHP’s inbuilt strip_tags function lets you remove all HTML tags from string and also provides you ability to ignore certain tags.

remove all html tags from string php

Strip_tags() is a function that allows you to strip out all HTML and PHP tags from a given string. I will learn to the example of clear HTML tags form a string in php. I will remove HTML tag using strip_tags() method in php.

Removing HTML from a string

To remove all HTML tags from a string, use the strip_tags function.

string strip_tags ( string source [, string allowable_tags])

Remove all html tags from php string

Here following example of remove HTML tags form a string in php.
using strip_tags

$text = '

Welcome Pakainfo.

Other text'; echo strip_tags($text); //output Welcome Pakainfo. Other text

PHP – How to Remove HTML Tags From a String?

to Tamilrokers";

echo strip_tags($html_string);
?>

Results:

Welcome to Tamilrokers.com

Using Userdefined Method

Welcome to Tamilrokers.com.Welcome to Devloper or Designer ";


$removed_str=remove_html_tags($data_content, array("span","b",'i'));

echo htmlentities($removed_str);
    
function remove_html_tags($data_content, $html_tags) 
{
    $tagStr = "";
  
        foreach($html_tags as $key => $value) 
        { 
            $tagStr .= $key == count($html_tags)-1 ? $value : "{$value}|"; 
        }
  
    $pat_str= array("/(<\s*\b({$tagStr})\b[^>]*>)/i", "/(<\/\s*\b({$tagStr})\b\s*>)/i");
    $result = preg_replace($pat_str, "", $data_content);
    return $result;
}
?>

Results

Welcome to Tamilrokers.com.Welcome to Devloper or Designer

Don’t Miss :

PHP | strip_tags() Function

Here are main 2 examples of stripping out tags:

Welcome!";
    $a = strip_tags($input);
    $b = strip_tags($input, "");
?>

I hope you get an idea about remove all html tags from string php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment