note 74526 deleted from function.strip-tags by bjori

From: Date: Tue, 17 Apr 2007 09:40:36 +0000
Subject: note 74526 deleted from function.strip-tags by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-125512@lists.php.net to get a copy of this message
Note Submitter: admin woof goodsoft point lv Reason: bad code ---- 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; } ?>

« previous php.notes (#125512) next »