1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <?php function rich_text_filter($content) { $allowTags = array('<br>', '<b>', '<p>', '<i>', '<u>', '<div>', '<strong>', '<img>'); $allowAttributes = array('src', 'width', 'style'); $allowCss = array("width", "text-align", "font-weight");
$content = strip_tags($content, implode($allowTags));
$content = preg_replace("/<\s*/", '<', $content);
$content = preg_replace_callback("/([a-zA-Z0-9\-]+)=['\"]([^'\"]*)['\"]\s*/i", function ($matches) use ($allowAttributes, $allowCss) { if (!isset($matches[1]) || !in_array(trim($matches[1]), $allowAttributes)) { return ' '; }
if (trim($matches[1]) === 'style' && isset($matches[2]) && !empty($matches[2])) { $styles = $matches[2] . (preg_match("/;$/", $matches[2]) ? '' : ';'); $styles = preg_replace_callback("/([^:;]+):([^:;]+);/", function ($items) use($allowCss) { if (isset($items[1]) && in_array(trim($items[1]), $allowCss)) { return $items[0]; } return ''; }, $styles);
return sprintf('style="%s" ', $styles); }
return sprintf(' %s ', $matches[0]); }, $content);
return $content; }
|