Tags
Asked 2 years ago
9 Jun 2021
Views 160
debugger

debugger posted

strip_tags removes too much in php ?

strip_tags removes too much in php ?
lain

lain
answered Apr 26 '23 00:00

Yes, strip_tags () function removes all HTML and PHP tags from the input string, which means that it removes not only the HTML tags, but also any PHP code that may be present within the string. This can sometimes lead to unexpected results, especially if the input string contains PHP code that is needed for the application to function properly.

To avoid this issue, you can use the optional second parameter of strip_tags () function, which allows you to specify a list of allowable tags to keep in the output string. Here's an example:



$string_with_tags = "<p>This is some <strong>text</strong> with <a href='#'>HTML</a> tags.</p>";
$allowed_tags = "<p><strong><a>";
$stripped_string = strip_tags($string_with_tags, $allowed_tags);
echo $stripped_string;

Output:



<p>This is some <strong>text</strong> with <a href='#'>HTML</a> tags.</p>

In this example, the second parameter of strip_tags () function is used to specify a list of allowed tags, which includes < p >, < strong >, and < a >. As a result, only those tags are retained in the output string, and all other tags are removed. This allows you to keep the necessary HTML tags in the output string while still removing any unwanted tags.






Regenerate response
Post Answer