Laman

Senin, 12 Desember 2011

Remove HTML Tags With Exception

Description :
Remove all html code but with exception



Function :
function removeHtmlTagsWithExceptions($html, $exceptions = null){
    if(is_array($exceptions) && !empty($exceptions))
    {
        foreach($exceptions as $exception)
        {
            $openTagPattern  = '/<(' . $exception . ')(\s.*?)?>/msi';
            $closeTagPattern = '/<\/(' . $exception . ')>/msi';

            $html = preg_replace(
                array($openTagPattern, $closeTagPattern),
                array('||l|\1\2|r||', '||l|/\1|r||'),
                $html
            );
        }
    }

    $html = preg_replace('/<.*?>/msi', '', $html);

    if(is_array($exceptions))
    {
        $html = str_replace('||l|', '<', $html);
        $html = str_replace('|r||', '>', $html);
    }

    return $html;
}

Tidak ada komentar:

Posting Komentar