Skip to content

Instantly share code, notes, and snippets.

@supermethod
Created April 11, 2011 11:24
Show Gist options
  • Select an option

  • Save supermethod/913378 to your computer and use it in GitHub Desktop.

Select an option

Save supermethod/913378 to your computer and use it in GitHub Desktop.

Revisions

  1. supermethod revised this gist Apr 11, 2011. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -23,4 +23,11 @@ function close_dangling_tags($html){
    }
    }
    return $html;
    }

    //remove html and wrap remaining in CDATA
    function removeHTML($string) {
    $string = strip_tags($string, '<p><span class="intro"><h3><h4><h5><li>');
    $string = close_dangling_tags($string);
    return '<![CDATA['.$string.']]>';
    }
  2. supermethod renamed this gist Apr 11, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. supermethod created this gist Apr 11, 2011.
    26 changes: 26 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    //from http://codesnippets.joyent.com/posts/show/959
    function close_dangling_tags($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;
    }