How to Strip All HTML Tags Except Specific Ones in PHP?

Susan Sarandon
Release: 2024-10-17 17:34:02
Original
484 people have browsed it

How to Strip All HTML Tags Except Specific Ones in PHP?

Removing All HTML Tags Except Permitted Ones in PHP

Users frequently encounter the need to remove specific or all HTML tags from a string. However, a unique scenario arises when only certain tags are to be retained while all others are stripped. In PHP, the built-in strip_tags function provides a solution for this requirement.

Solution:

The strip_tags function allows you to select specific tags that should be exempted from removal. By passing a string of the permitted tags as the second argument, you can strip all tags except those specified:

<code class="php">$allowed_tags = '&lt;code&gt;&lt;p&gt;';
$content = strip_tags($input, $allowed_tags);</code>
Copy after login

In this code:

  • $input is the original HTML string.
  • $allowed_tags is a string containing the tags that should be kept (e.g., and

    ).

  • $content is the resulting string with all tags stripped except for the allowed ones.

The above is the detailed content of How to Strip All HTML Tags Except Specific Ones in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!