Skip to content

Instantly share code, notes, and snippets.

@msonnabaum
Created December 14, 2010 21:46
Show Gist options
  • Save msonnabaum/741163 to your computer and use it in GitHub Desktop.
Save msonnabaum/741163 to your computer and use it in GitHub Desktop.
Strips out inline styles like '<span style="font-family:arial;font-size:13px">'. Arguments work similarly to strip_tags().
<?php
function strip_inline_styles($string, $allowed_styles = array()) {
$pattern = '/style="([^"\']+)"/';
$parsed = preg_replace_callback($pattern, function($match = NULL) use($allowed_styles) {
// Get style attribute values
$styles = $match[1];
// Parse each sytle out
$pattern2 = "/(?:([\w-]+?):([\w-]+);?)/";
$parsed_styles = preg_replace_callback($pattern2, function($match2 = NULL) use($allowed_styles, $styles) {
list($style, $key, $val) = $match2;
// Is the style in the allowed styles?
if (in_array($key, $allowed_styles)) {
return $style;
}
else {
return '';
}
}, $styles);
return (!empty($parsed_styles)) ? 'styles="' . $parsed_styles . '"' : '';
}, $string);
return $parsed;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment