note 74526 added to function.strip-tags

From: Date: Mon, 16 Apr 2007 00:40:20 +0000
Subject: note 74526 added to function.strip-tags
Groups: php.notes 
Request: Send a blank email to php-notes+get-125430@lists.php.net to get a copy of this message
I wrote this for stripping unwelcome tags, tag values and styles and closing unclosed tags. <?php $allowed = array ( 'a' => array('href', 'title'), 'b' => array(), 'br' => array(), 'hr' => array() ); $allowedStyles = array ( 'font-size', 'text-color' ); $singleTags = array( 'br', 'hr' ); function extractParams ($text) { $result = array(); while ($text) { if (strpos($text, '=') === FALSE) { $result[trim($text)] = ''; break; } $paramName = strtolower(trim(substr($text, 0, strpos($text, '=')))); $text = trim(substr($text, strpos($text, '=') + 1)); $param = ''; if (($text[0] == '"') || ($text[0] == "'")) { $quote = $text[0]; $text = substr($text, 1); $param = trim((strpos($text, $quote) !== FALSE) ? substr($text, 0, strpos($text, $quote)) : $text); $text = trim((strpos($text, $quote) !== FALSE) ? substr($text, strpos($text, $quote) + 1) : ''); } else { $param = trim((strpos($text, ' ') !== FALSE) ? substr($text, 0, strpos($text, ' ')) : $text); $text = trim((strpos($text, ' ') !== FALSE) ? substr($text, strpos($text, ' ') + 1) : ''); } $result[$paramName] = $param; } return $result; } function extractStyles ($text) { $styles = explode(';', $text); $result = array(); foreach ($styles as $style) { list($name, $value) = explode(':', $style, 2); $result[strtolower(trim($name))] = trim($value); } return $result; } function cleanHtml ($text) { global $allowed, $allowedStyles, $singleTags; $result = ''; $opened = array(); while ($text) { if (strpos($text, '<') === FALSE) { $result .= $text; break; } $plain = substr($text, 0, strpos($text, '<')); $text = substr($text, strpos($text, '<') + 1); $tag = trim((strpos($text, '>') !== FALSE) ? substr($text, 0, strpos($text, '>')) : $text); $text = substr($text, strlen($tag) + 1); $result .= $plain; $tagName = strtolower(trim((strpos($tag, ' ') !== FALSE) ? substr($tag, 0, strpos($tag, ' ')) : $tag)); $tagParams = trim((strpos($tag, ' ') !== FALSE) ? substr($tag, strpos($tag, ' ') + 1) : ''); $params = extractParams($tagParams); if ($tagName[0] == '/') { $tagName = substr($tagName, 1); if ($opened[$tagName]) { --$opened[$tagName]; $result .= '</'.$tagName.'>'; } } else if (isset($allowed[$tagName])) { $allowedParams = $allowed[$tagName]; $result .= '<'.$tagName; foreach ($allowedParams as $key) { if ($params[$key]) { $result .= ' '.$key.'="'.$params[$key].'"'; } } if ($params['style']) { $result .= ' style="'; $styles = extractStyles($params['style']); debug(print_r($styles, 1)); foreach ($allowedStyles as $key) { if ($styles[$key]) $result .= $key.': '.$styles[$key].';'; } $result .= '"'; } $result .= '>'; if (!isset($params['/']) && !in_array($tagName, $singleTags)) ++$opened[$tagName]; } } foreach ($opened as $tag => $count) $result .= str_repeat('</'.$tag.'>', $count); return $result; } ?> ---- Server IP: 81.94.227.69 Probable Submitter: 91.90.229.58 ---- Manual Page -- http://www.php.net/manual/en/function.strip-tags.php Edit -- https://master.php.net/note/edit/74526 Del: integrated -- https://master.php.net/note/delete/74526/integrated Del: useless -- https://master.php.net/note/delete/74526/useless Del: bad code -- https://master.php.net/note/delete/74526/bad+code Del: spam -- https://master.php.net/note/delete/74526/spam Del: non-english -- https://master.php.net/note/delete/74526/non-english Del: in docs -- https://master.php.net/note/delete/74526/in+docs Del: other reasons-- https://master.php.net/note/delete/74526 Reject -- https://master.php.net/note/reject/74526 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#125430) next »