PHP strip_tags関数:HTMLタグ除去の基本と応用
strip_tags() 関数は、文字列からHTMLやPHPのタグを削除するために使用されます。その構文は次のようになります:
string strip_tags ( string $str [, string $allowable_tags ] )
strとは、削除するタグの文字列です。allowable_tagsはオプションのパラメータであり、保持することができる許可されたタグを指定するために使用されます。
allowable_tags パラメーターを指定しない場合、関数は文字列内のすべてのタグを削除します。allowable_tags パラメーターを指定した場合、指定されたタグを保持し、他のタグは削除されます。
例えば、
$text = "<p>This is a <b>bold</b> and <i>italic</i> text.</p>";
echo strip_tags($text); // 输出:This is a bold and italic text.
echo strip_tags($text, "<b>"); // 输出:<p>This is a <b>bold</b> and italic text.</p>