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.

Read Also:  How To Work With Laravel 6 Flash Messages?

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 = '<p>Welcome Pakainfo.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);   //output Welcome Pakainfo. Other text

<?php echo substr(strip_tags($row_get_Business['business_description']),0,110) . "..."; ?>

PHP – How to Remove HTML Tags From a String?

<?php

$html_string="Welcome <span>to</span> <b>Tamilrokers</b>";

echo strip_tags($html_string);
?>

Results:

Welcome to Tamilrokers.com

Using Userdefined Method

<?php
$data_content = "<span>Welcome <i>to</i> Tamilrokers.com.</span><b>Welcome to Devloper or Designer </b>";


$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 :

Read Also:  Menu Based Program in PHP Using Switch case

PHP | strip_tags() Function

Here are main 2 examples of stripping out tags:

<?php
    $input = "<blink><strong>Welcome!</strong></blink>";
    $a = strip_tags($input);
    $b = strip_tags($input, "<strong><em>");
?>


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.