Tags
PHP , HTML , XHTML
Asked 3 years ago
9 Jun 2021
Views 463
jagdish

jagdish posted

how to remove all html tags from string in php ?

how to remove all html tags from string in php ?
ching

ching
answered Feb 27 '23 00:00

In PHP, you can use the strip_tags() function to remove all HTML and PHP tags from a string .

Here's an example code snippet:

$htmlString = '<p>This is a <b>sample</b> HTML string.</p>';
$plainString = strip_tags($htmlString);
echo $plainString;

Output:



This is a sample HTML string.


In this example, the strip_tags() function is used to remove all the HTML tags from the $htmlString variable and store the resulting plain text in the $plainString variable.

You can also specify which tags to allow by passing a second parameter to the strip_tags() function, which is an optional string containing a list of allowed tags. For example, if you want to allow <b> and <i> tags, you can use the following code:


$htmlString = '<p>This is a <b>sample</b> HTML string with <i>some italic text</i>.</p>';
$plainString = strip_tags($htmlString, '<b><i>');
echo $plainString;


Output:

This is a <b>sample</b> HTML string with <i>some italic text</i>.


In this example, the second parameter of strip_tags() function is set to allow only <b> and <i> tags, and all other tags are removed.



Post Answer