note 94948 added to function.strip-tags
| From: | Julian at osu1 dot php dot net | Date: | Thu, 03 Dec 2009 22:18:45 +0000 |
| Subject: | note 94948 added to function.strip-tags | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-163556@lists.php.net to get a copy of this message | ||
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
----
Server IP: 130.206.1.5
Probable Submitter: 88.6.20.121
----
Manual Page -- http://www.php.net/manual/en/function.strip-tags.php
Edit -- https://master.php.net/note/edit/94948
Del: integrated -- https://master.php.net/note/delete/94948/integrated
Del: useless -- https://master.php.net/note/delete/94948/useless
Del: bad code -- https://master.php.net/note/delete/94948/bad+code
Del: spam -- https://master.php.net/note/delete/94948/spam
Del: non-english -- https://master.php.net/note/delete/94948/non-english
Del: in docs -- https://master.php.net/note/delete/94948/in+docs
Del: other reasons-- https://master.php.net/note/delete/94948
Reject -- https://master.php.net/note/reject/94948
Search -- https://master.php.net/manage/user-notes.php