Tag closing for HTML text pieces


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

closes opened tags in text piece.
original code slightly modified to supress PHP warnings

NOTE: need to fix the order of closing tags to get standards compliance


Copy this code and paste it in your HTML
  1. function closetags($text){
  2. preg_match_all("#<([a-zA-Z]{1,50})[^>]*>#i",$text,$otags);
  3. if(count($otags[1])>0){
  4. $fotags=array();
  5. $fctags=array();
  6. foreach($otags[1] as $otag){
  7. $otag = strtolower($otag);
  8. if(isset($fotags[$otag]))
  9. $fotags[$otag]++;
  10. else
  11. $fotags[$otag] = 1;
  12. }
  13. preg_match_all("#</([a-zA-Z]{1,50})#i",$text,$ctags);
  14. foreach($ctags[1] as $ctag){
  15. $ctag = strtolower($ctag);
  16. if(isset($fctags[$ctag]))
  17. $fctags[$ctag]++;
  18. else
  19. $fctags[$ctag] = 1;
  20. }
  21. $rtags = array_reverse($fotags, TRUE);
  22. foreach ( $rtags as $tag => $cnt)
  23. {
  24. $fctags[$tag] = isset($fctags[$tag]) ? $fctags[$tag] : 0;
  25. $text .= str_repeat("</".$tag.">",abs($fctags[$tag] - $cnt));
  26. }
  27. }
  28. $wrong_replace = array(
  29. '</br>' => '',
  30. '</img>' => '',
  31. '</hr>' => '',
  32. '</link>' => '',
  33. '</meta>' => ''
  34. );
  35. $text=strtr($text, $wrong_replace);
  36. return $text;
  37. }

URL: http://www.promoforum.ru/index.php?showtopic=12687&view=findpost&p=97701

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.