PHPRO.ORG

The PHP strip_tags function does a good job to remove HTML or XML tags from a string. It even provides an allowable tags arguement to permit the tags you do not want stripped. This function works the opposite way, by specifying an array of tags that DO need to be stripped.

The function takes two arguements, the first is the array of tags to be stripped, and the second is the string that you want stripped. Of course, the array can have a single tag if you need to strip a single HTML tag, or XML tag.


<?php

/**
 *
 * @strip a single HTML/XML tag from a string
 *
 * @param array $tags The tags to strip
 *
 * @param string The string to strip from
 *
 * @return string
 *
 */
function stripSingleTags($tags$string)
{
    foreach( 
$tags as $tag )
    {
        
$string preg_replace('#</?'.$tag.'[^>]*>#is'''$string);
    }
    return 
$string;
}

/*** example usage ***/
$string '<p>stuff</p><span>more <span class="foo">and even>< more</span> stuff here</span>';

$tags = array('h1''span');

echo 
stripSingleTags($tags$string);
?>

Demonstration

stuff

more and even>< more stuff here