What is the use of strip_tags() in PHP?
The strip_tags() function removes HTML and PHP tags from a string. Only the tags are removed, the text inside them stays.
<?php
echo strip_tags("<b>Hello</b> <i>PHP</i>");
?>
Output:
Tutorials Class - Output Window
Hello PHP
You can keep some tags by passing them as second parameter. Here we allow only the bold tag, and we use htmlspecialchars() just to show the HTML code as text.
<?php
$text = "<p><b>Hello</b> <i>PHP</i></p>";
echo htmlspecialchars(strip_tags($text, "<b>"));
?>
Output:
Tutorials Class - Output Window
<b>Hello</b> PHP
Note:
strip_tags() is not a full protection for XSS. The text inside a script tag stays as text, and attributes of allowed tags are not checked. To print user data safely use htmlspecialchars(), see PHP Security Tips.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |