Select Page

To strip all HTML tags inside a string in PHP, you can use the strip_tags() function with no second argument, or an empty string as the second argument. Here’s an example:

$str = "<p>This is a <b>sample</b> string with <a href='#'>an anchor tag</a></p>";
$newStr = strip_tags($str);

echo $newStr; // Output: "This is a sample string with an anchor tag"

In this example, the strip_tags() function takes one argument, which is the original string $str. Since we don’t specify any allowed tags in the second argument or pass an empty string, all HTML tags inside the string will be stripped.

The resulting string $newStr will have all the HTML tags removed. Note that any content inside the removed tags will also be removed.

To allow only particular tags like <p> tags and <a> tags from a string in PHP, you can use the strip_tags() function with an allowed tags parameter. Here’s an example:

$str = "<p><div>This</div> is a <span>sample string</span> with <a href='#'>an anchor tag</a></p>";
$newStr = strip_tags($str, "<p><span>");

In this example, the strip_tags() function takes two arguments. The first argument is the original string $str. The second argument is a string containing a list of allowed tags that you want to keep in the output. In this case, we only allow <p> and <span> tags, so all <div> and <a> tags will be removed.

The resulting string $newStr will have all the <div> and <a> tags removed. Note that any content inside the removed tags will also be removed.