How to Use strip_tags() Function in PHP 8.4 and PHP 8.5?

The strip_tags() function in PHP 8.4 and PHP 8.5 works exactly the same as in previous versions. There are no version-specific changes to its behavior or signature in these releases. It remains a tool for removing HTML and PHP tags from a string, but it is not a security function for preventing XSS attacks.

Syntax

The function signature is consistent across PHP 8.x :

strip_tags(string $string, array|string|null $allowed_tags = null): string

Here is a basic example of how it works:

<?php
$text = <p>Hello <b>World</b>!</p> ;

// Remove all tags
echo strip_tags($text);
// Output: Hello World!

// Allow only the <p> tag (string format)
echo strip_tags($text, <p> );
// Output: <p>Hello World!</p>

// Allow tags using an array (PHP 7.4+)
echo strip_tags($text, [ p ]);
// Output: <p>Hello World!</p>
?>

Recent Changes (Prior to 8.4)

1.While 8.4 and 8.5 introduced no changes, the function did evolve recently:

2.PHP 8.0.0: The $allowed_tags parameter became nullable.
3.PHP 7.4.0: The $allowed_tags parameter started accepting an array, which is often cleaner than the string format.

If you are looking for a specific example of handling a particular type of string or a safer alternative for your use case, feel free to ask.

5 Views