ホームページ  >  記事  >  バックエンド開発  >  PHPで特定のタグを除くすべてのHTMLタグを削除するにはどうすればよいですか?

PHPで特定のタグを除くすべてのHTMLタグを削除するにはどうすればよいですか?

Susan Sarandon
Susan Sarandonオリジナル
2024-10-17 17:34:02477ブラウズ

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>

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.

以上がPHPで特定のタグを除くすべてのHTMLタグを削除するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。