繁体   English   中英

用于替换HTML代码中CSS类的正则表达式

[英]Regular expression for replacing css classes in html code

我在php中寻找preg_replace的正则表达式,用最小的类名替换html文件中的类名。 我这样做是在最小化CSS的过程中进行的。 我得到了一个关联数组,其中类名作为键,替换项作为值。 即:

$myReplacements = array('fonts' => 'f', 'label' => 'l', 'tiny' => 't')

这些替换只能在完全匹配的情况下进行,而不能在“ fonts-small-size”之类的类上进行。 我的正则表达式是:

/"((.*[^"]?)?(\} | |\}))?fonts(( \{| |\{)(.*[^"])?)?"/

使用replaceregex:

"$2$3f$5$6"

-

我得到了带有替换的第二个关联数组,对于仅以它开头的类也应该这样做:

$forcedReplacements = array('ui-icon-' => 'ui-')

此替换应在“ ui-icon-thumbs-up”之类的类上进行,并应替换为“ ui-thumbs-up”。 我的正则表达式是:

/"(.*)ui-icon-(.*)"/

使用replaceregex:

"$1ui-$2"

我要替换此类名称的HTML文件具有以下内容:

{if !$isSmallFontCheckDisabled}
    <span class="{if $smallFontFromCharacters}fonts tiny{/if}{if $hasStandardLabel} fonts label{/if}">
{/if}

这是我的模板文件之一的小片段。 如您所见,我将smarty用作模板引擎。 因此,在我的正则表达式中也必须考虑聪明的语法。

在大多数情况下,替换产品效果很好。 如果我的模板文件的class属性包含两次相同的类,就会出现问题(如果我得到了if / else-smarty-block,可能会发生这种情况)。 然后,仅替换其中之一。

上面的模板代码段被替换为:

{if !$isSmallFontCheckDisabled}
    <span class="{if $smallFontFromCharacters}fonts t{/if}{if $hasStandardLabel} f l{/if}">
{/if}

有人可以用我的正则表达式来帮助我替换所有出现的模板吗?

您不想使用preg_replace,而是想使用preg_replace_callback()。 将class属性从标记中拉出,在空白处分割,处理结果数组的每个元素,然后将其粘贴回去。 像这样

$line = preg_replace_callback(
    '/(<[^<] class=\")([^"]*)(\")/',
    function ($matches) {
        $classes = explode(' ', $matches[2])
            /*
                Do your class name replacements here
            */
        return $matches[1].(implode(' ', $classes)).$matches[3];
    },
    $line
);

希望这可以帮助...

我的用例:

将附加(css)类附加到html标签的class属性

演示: https : //regex101.com/r/YZ1Nre/2

正则表达式:

/(?<opening_tag><input)(?<attributes_before_class>.*)(?<class_attribute_start>class=\')(?<class_value>.*?)(?<class_attribute_end>\')(?<attributes_after_class>.*)(?<closing_tag>\/>)/

替代:

${opening_tag}${attributes_before_class}${class_attribute_start}${class_value} uk-width-1-1 ${class_attribute_end}${attributes_after_class}${closing_tag}

测试字符串:

  <input name='input_4' id='input_2_4' type='text' value='' class='large'  tabindex='1'   aria-required="true" aria-invalid="true" />

结果:

 <input name='input_4' id='input_2_4' type='text' value='' class='large uk-width-1-1'  tabindex='1'   aria-required="true" aria-invalid="true" />


PHP代码:

$re = '/(?<opening_tag><input)(?<attributes_before_class>.*)(?<class_attribute_start>class=\')(?<class_value>.*?)(?<class_attribute_end>\')(?<attributes_after_class>.*)(?<closing_tag>\/>)/';
$str = 'Your test string here';
$new_css_class = 'uk-width-1-1';

$subst = '${opening_tag}${attributes_before_class}${class_attribute_start}${class_value} '.$new_css_class.' ${class_attribute_end}${attributes_after_class}${closing_tag}';

// if the named group replacing doesnt work, just use the indices as references
//$subst = '${1}${2}${3}${4} uk-width-1-1 ${5}${6}${7}';

$result = preg_replace($re, $subst, $str);

echo "The result of the substitution is ".$result;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM