A, definitions and usage
The Strip_tags () function removes the HTML, XML, and PHP tags in the string.
Strip_tags (String,allow)
Parameter description
String required. Specify the string to check.
Allow optional. Specify the allowed label. These labels will not be deleted.
Note: This function always strips HTML comments. This cannot be changed by allow parameters.
B, example
Example 1
Echo strip_tags ("Hello <b>world!</b>");
Output:
Hello world!
Example 2
Echo strip_tags ("Hello <b><i>world!</i></b>", "<b>");
Output:
Hello world!
Example 3, strip_tags () example
<?php$text = ' <p>test paragraph.</p><!--Comment--> other text '; Echo strip_tags ($text); echo "\ n" ;//Allow <p>echo strip_tags ($text, ' <p> '); >
The example above will output:
Test paragraph. Other text
<p>test paragraph.</p> Other text
Example 4:
<?php
$str = "Hello <b><i>world</i></b>!";
echo Strip_tags ($STR);
echo ' <br/> ';
Echo strip_tags ($str, ' <b><i> ');
?>
Output results:
Hello world!
Hello world!
Example 5
What if you want to use PHP to remove a specific label from an HTML tag?
This will require code to implement, as follows:
function strip_selected_tags ($text, $tags = Array ())
{
$args = Func_get_args ();
$text = Array_shift ($args);
$tags = Func_num_args () > 2 Array_diff ($args, Array ($text)): (Array ) $tags;
foreach ($tags as $tag) {
if (preg_match_all ('/< '. $tag. ') [^>]*> ([^<]*) </'. $tag. ' >/iu ', $text, $found)) {
$text = str_replace ($found [0], $found [1], $text);
}
}
return Preg_replace (' < (') join (' | ', $tags). ( | |.) */>)/iu ', ', $text);
}
$str = "[url="]123[/url] ";
Echo strip_selected_tags ($str, Array (' B '));