在PHP中将CSS白名单应用于HTML

时间:2011-07-21 23:40:52

标签: php cakephp

假设我有以下$string...

<span style='text-decoration:underline; display:none;'>Some text</span>

我只想允许样式text-decoration,所以我想要一个像以下那样的PHP函数......

$string = stripStyles($string, array("text-decoration"));

strip_tags类似,但改为使用数组。所以$string现在将......

<span style='text-decoration:underline;'>Some text</span>

我正在使用Cake,所以如果可以使用Sanitize,那就更好了。

1 个答案:

答案 0 :(得分:1)

这很棘手,但您应该可以使用DOMDocument执行此操作。这应该让你开始,但它可能需要一些严肃的调整。

// Load your html string
$dom = new DOMDocument();
$dom->loadHTML($your_html_string);

// Get all the <span> tags
$spans = $dom->getElementsByTagName("span");

// Loop over the span tags
foreach($spans as $span) {

  // If they have a style attribute that contains "text-decoration:"
  // attempt to replace the contents of the style attribute with only the text-decoration component.
  if ($style = $span->getAttribute("style")) {
    if (preg_match('/text-decoration:([^;]*);/i', $style)) {
      $span->setAttribute("style", preg_replace('/^(.*)text-decoration:([^;]*);(.*)$/i', "text-decoration:$2;", $style);
    }
    // Otherwise, erase the style attribute
    else $span->setAttribute("style", "");
  }
}

$output = $dom->saveHTML;

尝试通过explode()

;来解析样式属性可能会更好
// This replaces the inner contents of the foreach ($spans as $span) above...

// Instead of the preg_replace()
$styles = explode(";", $style);
$replaced_style = FALSE;
foreach ($styles as $s) {
 if (preg_match('/text-decoration/', $s) {
   $span->setAttribute("style", $s);
   $replaced_style = TRUE;
 }
 //  If a text-decoration wasn't found, empty out the style
 if (!$replaced_style) $span->setAttribute("style", "");
}