[TYPO3-dev] cropHtml as new Feature for stdWrap

Denyer Ec denyerec at gmail.com
Tue Mar 6 00:50:52 CET 2007


I have been using this:

<?php
// Functions for cropping HTML without breaking tags
// EG to run as a postUserFunc when cropping news articles for the frontpage

class user_htmlCrop{

function user_htmlCrop(){
}

/**
* close all open xhtml tags at the end of the string
*
* @param string $html
* @return string
* @author Milian Wolff <mail at milianw.de>
*/
function closetags($html){

  #put all opened tags into an array
  preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU",$html,$result);
  $openedtags=$result[1];

  #put all closed tags into an array
  preg_match_all("#</([a-z]+)>#iU",$html,$result);
  $closedtags=$result[1];
  $len_opened = count($openedtags);

  # all tags are closed
  if(count($closedtags) == $len_opened){
    return $html;
  }
  $openedtags = array_reverse($openedtags);
  # close tags
  for($i=0;$i < $len_opened;$i++) {
    if (!in_array($openedtags[$i],$closedtags)){
      $html .= '</'.$openedtags[$i].'>';
      } else {
      unset($closedtags[array_search($openedtags[$i],$closedtags)]);
     }
  }
return $html;
}

}
?>

Though I am in no position to say whether it is better / worse / safe /
efficient / sensible / sane.
It just appears to work and fix dodgy markup when cropping.
(Google mail broke the indenting, sorry!!)
Denyerec




More information about the TYPO3-dev mailing list