Created
December 14, 2010 21:46
-
-
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().
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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