This article introduces the method of PHP strip_tags function to retain multiple HTML tags. You can use the second parameter to set The tags that need to be deleted mainly involve the second parameter of strip_tags
strip_tags function
Grammar
string strip_tags ( string str [, string allowable_tags] )
Returns a string with HTML tags removed; you can use the second parameter to set tags that do not need to be removed.
How to use:
Premise: If there is such a string now,
Copy code The code is as follows:
$str = "
I am fromBangke Home
1, do not retain any HTML tags, the code will be like this:
Copy code The code is as follows:
echo strip_tags($str);
// Output: I am from Bangke Home
2. If you only retain one tag , you only need to write the string into the second parameter of strip_tags:
Copy code The code is as follows:
echo strip_tags($str, "");
// Output: I am fromBangke Home
3. To retain multiple tags of
and ..., you only need to separate multiple tags with spaces and write them to the second parameter of strip_tags:
Copy code The code is as follows:
echo strip_tags($str, "
");
// Output:
I am fromBang Ke Home
What if you want to remove specific tags in html tags using php?
This requires 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'));