note 93567 deleted from function.strip-tags by aharvey

From: Date: Thu, 20 Sep 2012 12:11:57 +0000
Subject: note 93567 deleted from function.strip-tags by aharvey
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190920@lists.php.net to get a copy of this message
Note Submitter: Steve ---- Here is a function like strip_tags, only it removes only the tags (with attributes) specified: <?php function strip_only($str, $tags) { if(!is_array($tags)) { $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags)); if(end($tags) == '') array_pop($tags); } foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str); return $str; } $str = '<p style="text-align:center">Paragraph</p><strong>Bold</strong><br/><span style="color:red">Red</span><h1>Header</h1>'; echo strip_only($str, array('p', 'h1')); echo strip_only($str, '<p><h1>'); ?> Both return: Paragraph<strong>Bold</strong><br/><span style="color:red">Red</span>Header Hope this helps somebody else

« previous php.notes (#190920) next »