PHP strip_tags() removes HTML and PHP tags from a string, optionally retaining named HTML tags. The examples below show how the call behaves, where it is useful, and which limitations matter.
What Is strip_tags() in PHP?
Removes HTML and PHP tags from a string, optionally retaining named HTML tags.
Its call structure is:
strip_tags($string, [$allowed_tags])
The arguments identify the values or resources passed to strip_tags(). Read the examples with attention to the type of each supplied argument and to whether the function modifies its input or returns a separate result.
Example 1: Basic Usage
This example introduces strip_tags with a direct call and a small, readable input.
<?php
echo strip_tags("<b>Hello</b> <i>world</i>");
Output:
Hello world
Removes HTML and PHP tags from a string, optionally retaining named HTML tags. It is not an HTML sanitizer and does not make retained attributes safe.
Example 2: Another Behavior or Input
Compare this input with the previous example: changing the data or option can affect how strip_tags behaves.
<?php
echo strip_tags("<p><strong>Hello</strong></p>", "<strong>");
Output:
<strong>Hello</strong>
The changed input illustrates why output may differ with the chosen values or environment. It is not an HTML sanitizer and does not make retained attributes safe.
Practical Example: Using the Feature
The following example incorporates strip_tags into a workflow. Values such as paths, connection handles and configuration options must come from trusted application settings.
<?php
$comment = "<script>alert(1)</script>hello"; echo htmlspecialchars(strip_tags($comment), ENT_QUOTES, "UTF-8");
Output:
alert(1)hello
In a real application, check each operation result, validate values before use and clean up acquired resources. It is not an HTML sanitizer and does not make retained attributes safe.
Important Behavior and Edge Cases
- Purpose: Removes HTML and PHP tags from a string, optionally retaining named HTML tags.
- Key limitation: It is not an HTML sanitizer and does not make retained attributes safe.
- Failure handling: Check return values and handle invalid inputs and unavailable operating-system or extension features where relevant.
Comparison With Related Functions
| Function or Approach | Best Fit |
|---|---|
strip_tags() | Removes HTML and PHP tags from a string, optionally retaining named HTML tags. |
htmlspecialchars() | Encodes HTML special characters for output; strip_tags() is not a replacement for safe HTML escaping. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| It is not an HTML sanitizer and does not make retained attributes safe. | The correct approach depends on this behavior: It is not an HTML sanitizer and does not make retained attributes safe. |
| Ignoring errors or cleanup after the operation. | Validate inputs and check each result; close files, restore settings or release resources when the operation requires it. |
Key Takeaways
- Removes HTML and PHP tags from a string, optionally retaining named HTML tags.
- It is not an HTML sanitizer and does not make retained attributes safe.
- Use the examples as starting points, and adapt environmental paths, resources and permissions rather than assuming fixed results.
strip_tags is useful when its specific behavior fits the problem. The safest implementations combine the correct API with input validation, error checking and resource cleanup.