Close unclosed tags


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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. ?>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.