Search This Blog

Loading...

Thursday, December 8, 2011

Using simple_html_dom to remove all the spans from a snippet of HTML

function cleanSpansRecursive(&$body) {

    $HTML = new simple_html_dom;
    $HTML->load($body); 
    $spans = $HTML->find('span');
    foreach($spans as $span_tag) {
        $span_tag->outertext = (string)$span_tag->innertext;
    }

    $body =  (string)$HTML;
    if($HTML->find('span')) {
        $HTML->clear();
        unset($HTML);
        cleanSpansRecursive($body);
    } else {
        $HTML->clear();
        unset($HTML);
    }  }
And it seems to work.

0 comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails