note 94948 deleted from function.strip-tags by danbrown

From: Date: Fri, 04 Dec 2009 14:57:27 +0000
Subject: note 94948 deleted from function.strip-tags by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-163577@lists.php.net to get a copy of this message
Note Submitter: Julian ---- Inspired by previous Steve strip_only() posted function I've developed a more extensive function to strip specified tags as long as every single HTML tag from string (hope every tag is on the list) Works for HTML and XHTML tag versions, also for HTML comments, and is ready for the new HTML5 tags to be released <?php /** * Removes HTML tags from string. * If no tags specified in $tags removes every HTML tag on the string. * * @param string $string * @param mixed $tags * @return string */ function stripHtml($string, $tags = '') { if ($tags == '') { //current (HTML5) and previous HTML tags $tags = array( '!--', '!doctype', 'a', 'abbr', 'acronym', 'address', 'applet', 'area', 'article', 'aside', 'audio', 'b', 'base', 'basefont', 'bdo', 'big', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'center', 'cite', 'code', 'col', 'colgroup', 'command', 'datalis', 'dd', 'del', 'details', 'dialog', 'dfn', 'div', 'dir', 'dl', 'dt', 'em', 'embed', 'fieldset', 'figure', 'font', 'footer', 'form', 'frame', 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hgroup', 'hr', 'html', 'i', 'frame', 'img', 'input', 'ins', 'isindex', 'keygen', 'kbd', 'label', 'legend', 'li', 'link', 'map', 'mark', 'menu', 'meta', 'meter', 'nav', 'noframes', 'noscript', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'section', 'select', 'small', 'source', 'span', 'strike', 'strong', 'style', 'sub', 'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'tt', 'u', 'ul', 'var', 'video', 'xmp' ); } if (!is_array($tags)) { $tags = (strpos($tags, '>') !== false ? explode('>', str_replace(array('<', '/'), '', trim($tags))) : array(trim($tags))); if (end($tags) == '') { array_pop($tags); } } foreach($tags as $tag) { $tag = strtolower(trim($tag)); switch ($tag) { case '!comment': case 'comment': case '!--': $string = preg_replace('#<!--[^(-->)]*-->#is', '', $string); break; case '!doctype': case 'doctype': $string = preg_replace('#<!DOCTYPE[^>]*>#is', '', $string); break; default: $string = preg_replace('#</?'.$tag.'[^>]*/?>#is', '', $string); break; } } return $string; } ?> Hoping someone can find this useful as I did

« previous php.notes (#163577) next »