9 August 2012

Strip tags in PHP strips all tags, except allowed tags. This function strips tags that you define. Also you can define if you want to strip content as well.

Source code viewer
  1. function strip_defined_tags($str, $tags, $stripContent = false) {
  2. $content = '';
  3. if (!is_array($tags)) {
  4. $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
  5. if(end($tags) == '') array_pop($tags);
  6. }
  7. foreach($tags as $tag) {
  8. if ($stripContent) {
  9. $content = '(.+</'.$tag.'(>|\s[^>]*>)|)';
  10. $str = preg_replace('#</?'.$tag.'(>|\s[^>]*>)'.$content.'#is', '', $str);
  11. }
  12. return $str;
  13. }
Programming Language: PHP