Home > Article > Backend Development > PHP strip_tags method to retain multiple HTML tags, strip_tags tag_PHP tutorial
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 = "e388a4556c0f65e1904146cc1a846beeI am froma4b561c25d9afb9ac8dc4d70affff419305d4f929f21f7e9ebcdea53ac7dafd2Bangke Home5db79b134e9f6b82c0b36e0489ee08ed0d36329ec37a2cc24d42c7229b69747a51b364ec7b505771ce85379ba043e0e6, you only need to write the 3499910bf9dac5ae3c52d5ede7383485 string into the second parameter of strip_tags:
Copy code The code is as follows:
echo strip_tags($str, "3499910bf9dac5ae3c52d5ede7383485");
// Output: I am from305d4f929f21f7e9ebcdea53ac7dafd2Bangke Home5db79b134e9f6b82c0b36e0489ee08ed
3. To retain multiple tags of e388a4556c0f65e1904146cc1a846bee and a4b561c25d9afb9ac8dc4d70affff419..., 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, "e388a4556c0f65e1904146cc1a846bee a4b561c25d9afb9ac8dc4d70affff419");
// Output: e388a4556c0f65e1904146cc1a846beeI am froma4b561c25d9afb9ac8dc4d70affff419Bang Ke Home0d36329ec37a2cc24d42c7229b69747a94b3e26ee717c64999d7867364b1b4a3
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'));