Posted By

ginoplusio on 02/24/10


Tagged

fix xhtml xml tags


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

ginoplusio


Close unclosed tags


 / Published in: PHP
 

URL: http://www.barattalo.it/

  1. <?php
  2. /**
  3. * Find and close unclosed xml tags
  4. **/
  5. function close_tags($text) {
  6. $patt_open = "%((?<!</)(?<=<)[\s]*[^/!>\s]+(?=>|[\s]+[^>]*[^/]>)(?!/>))%";
  7. $patt_close = "%((?<=</)([^>]+)(?=>))%";
  8. if (preg_match_all($patt_open,$text,$matches))
  9. {
  10. $m_open = $matches[1];
  11. if(!empty($m_open))
  12. {
  13. preg_match_all($patt_close,$text,$matches2);
  14. $m_close = $matches2[1];
  15. if (count($m_open) > count($m_close))
  16. {
  17. $m_open = array_reverse($m_open);
  18. foreach ($m_close as $tag) $c_tags[$tag]++;
  19. foreach ($m_open as $k => $tag) if ($c_tags[$tag]--<=0) $text.='</'.$tag.'>';
  20. }
  21. }
  22. }
  23. return $text;
  24. }
  25. ?>

Report this snippet  

You need to login to post a comment.