note 93567 added to function.strip-tags

From: Date: Wed, 16 Sep 2009 17:03:02 +0000
Subject: note 93567 added to function.strip-tags
Groups: php.notes 
Request: Send a blank email to php-notes+get-160751@lists.php.net to get a copy of this message
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 ---- Server IP: 69.147.83.197 Probable Submitter: 12.4.141.8 ---- Manual Page -- http://www.php.net/manual/en/function.strip-tags.php Edit -- https://master.php.net/note/edit/93567 Del: integrated -- https://master.php.net/note/delete/93567/integrated Del: useless -- https://master.php.net/note/delete/93567/useless Del: bad code -- https://master.php.net/note/delete/93567/bad+code Del: spam -- https://master.php.net/note/delete/93567/spam Del: non-english -- https://master.php.net/note/delete/93567/non-english Del: in docs -- https://master.php.net/note/delete/93567/in+docs Del: other reasons-- https://master.php.net/note/delete/93567 Reject -- https://master.php.net/note/reject/93567 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#160751) next »