23 July 2012

This function strips tags by any identifier without regexp, and without heavy XML parsing. As long as the tag looks exactly the same in source-code it works.

Source code viewer
  1. /**
  2.  * Strip tags by any identifier without regexp, and without heavy XML parsing.
  3.  *
  4.  * @param string $html Input HTML
  5.  * @param string $tag Tag that is removed, example: <span class="some_class">
  6.  * @param string $tag_close Closing of tag, example: </span>
  7.  * @param string $tag_family Tag family, example: <span
  8.  * @param bool $del_children Will it remove tag and it's ending or everything between the tag aswell
  9.  * @return Processed HTML
  10.  */
  11. function strip_given($html, $tag = '<div>', $tag_close = '</div>', $tag_family = '<div', $del_children = TRUE) {
  12. $tag_len = strlen($tag);
  13.  
  14. $p1 = -1;
  15. $p2 = 0;
  16. while ( ($p2! == false) && (($p1 = strpos($html, $tag, $p1+1)) !== false) ) {
  17. $level = 1;
  18. $p2 = $p1;
  19. $continue = true;
  20. while ($continue) {
  21. $p2 = strpos($html, $tag_close, $p2+1);
  22. if ($p2 === false) {
  23. $continue = false;
  24. $p2 = false;
  25. } else {
  26. $level = $level -1;
  27. $x = substr($html, $p1+$tag_len, $p2-$p1-$tag_len);
  28. $n = substr_count($x, $tag_family);
  29. if ($level+$n<=0) $continue = false;
  30. }
  31. }
  32. if ($p2 !== false) {
  33. // remove tag and its ending
  34. if($del_children === FALSE) {
  35. $html = substr_replace($html, '', $p2, strlen($tag_close));
  36. $html = substr_replace($html, '', $p1, $tag_len);
  37. }
  38. // delete tag contents aswell
  39. else {
  40. $html = substr_replace($html, '', $p1, $p2+($p1-strlen($tag_close)));
  41. }
  42. }
  43. }
  44.  
  45. return $html;
  46. }
  47.  
  48. //------------------------------------------------------------------------------------------------------------------
  49.  
  50. /**
  51.  * Strip tags with regex and without heavy XML parsing.
  52.  *
  53.  * @param string $str Input HTML
  54.  * @param string $tags Tag that is removed, example: span class="some_class"
  55.  * @param bool $stripContent Strip contesnt aswell
  56.  * @return Processed HTML
  57.  */
  58. function strip_given($str, $tags, $stripContent = false) {
  59. $content = '';
  60. if (!is_array($tags)) {
  61. $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
  62. if(end($tags) == '') array_pop($tags);
  63. }
  64. foreach ($tags as $tag) {
  65. if ($stripContent) $content = '(.+</'.$tag.'(>|\s[^>]*>)|)';
  66. $str = preg_replace('#</?'.$tag.'(>|\s[^>]*>)'.$content.'#is', '', $str);
  67. }
  68. return $str;
  69. }
Programming Language: PHP