正则表达式删除背景颜色和颜色 属性 但保留所有样式 php

RegEx to remove background color and color property but leave all styles in php

我仍然无法记住正则表达式,因此无法找到最终的解决方案来使用 RegEx 从 table 中去除背景颜色、字体系列和颜色 属性,但是保留所有样式

我已尝试使用正则表达式删除除颜色和背景之外的所有样式 属性。 但实际上我想删除背景颜色、字体系列和颜色 属性 但保留所有样式

if(isset($_REQUEST["Save"])) {
    $VariantName = $_REQUEST["name"];
    $desc = preg_replace('/(<[^>]+\s+)(?:style\s*=\s*"(?!(?:|[^"]*[;\s])color\s*:[^";]*)(?!(?:|[^"]*[;\s])background-color\s*:[^";]*)[^"]*"|(style\s*=\s*")(?=(?:|[^"]*[;\s])(color\s*:[^";]*))?(?=(?:|[^"]*)(;))?(?=(?:|[^"]*[;\s])(background-color\s*:[^";]*))?[^"]*("))/i', '', $VariantName);
    echo $desc;
}

这个

<p style="font-family:Garamond;font-size:8px;line-height:14px;color:#FF0000;background-color:red;">example</p>

应该变成:

<p style="font-size:8px;line-height:14px;">example</p>

我不推荐这样做,但你可以使用这个正则表达式来删除你想要的 CSS 道具:

/(background-color|color|font-family)\:\#?\w+\;/i

示例:

<?php
            //Enter your code here, enjoy!
    $string = '<p style="font-family:Garamond;font-size:8px;line-height:14px;color:#FF0000;background-color:red;">example</p>';
    $pattern = '/(background-color|color|font-family)\:\#?\w+\;/i';
    $replacement = '';
    echo preg_replace($pattern, $replacement, $string);