提问人:Alex Kizyma 提问时间:1/31/2023 最后编辑:mickmackusaAlex Kizyma 更新时间:2/1/2023 访问量:41
使用 preg_replace() 替换部分 css 文件内容 [closed]
Replace portion of css file content using preg_replace() [closed]
问:
$settings['new_primary_colour_ccs'] = '#777777'; // test color
foreach($appPats as $path){
$styleSettings = [
'primary_colour' => [
'reg' => "\bprimaryColour:\s?['\"][a-z0-9 ,._+;()'@!?&#:\/-]+['\"]",
'string' => "primaryColour: \"".$settings['new_primary_colour_ccs']."\"",
'value' => $settings['new_primary_colour_ccs']
]
];
foreach ($styleSettings as $style) {
$content = file_get_contents($path);
$result = preg_replace($style['reg'], $style['string'], $content);
file_put_contents($path, $result);
var_dump($result);
}
}
我的 css 文本如下所示:
(n["createcCommentVNode"])("",!0)],64)})),256))])):Object
(n["createCommentVNode"])("", !0)])]), Object(n["createELementVNode"])("div"
,me,[Object(n["createELementVNode"])("div",be,[X.config.EntryFormWithoutTime
?(Object(n["openBlock"])(),Object(n["createBlock"])(te,{key:0,onUpdated:e
.onUpdated,"is-new":"",modelValue:R.newEntry,"onUpdate:modelValue":t[0]||
(t[0]=function(e){return R.newEntry=e})},null,8,["onUpdated","modelValue"]
)):(Object(n["openBlock"])(),Object(n["createBlock"])(xe,{key:1,onUpdated:e
.onUpdated,"is-new":"",modelValue:R.newEntry,"onUpdate:modelValue":t[1]||
(t[1]=function(e){return R.newEntry=e})},null,8,["onUpdated","modelValue"]
))])])],64)}a("498a"),a("acl1f"),a("5319"),a("a434");var ge={client:"Rail
Partners",showWelcomeMessage:!1,logo:"https: //1wee.webx.host/images
/RP_Logo_Black.svg",LogoWidth:124,LogoMargin:"10px 0 10px 0"
,navBackgroundColour:"rgb(36, 142, 120)",navShowWave:!1,colourScheme:"blue"
,bodyBackgroundImage:!1,bgColour:"#FFFFFF",primaryColour:"#AD9DF4",url
:"https://1wee.webx.host",enableAcademy:!0,enableScheduler:!0
,homepageDispLayVehicLeChart:!1,calendarDispLayNameOn1ly:!0
,caLendarMonthyDefaultSort:"name",EntryFormWithoutTime:!0
,caLendarAnnualLeaveCatId:25,calendarRemainingTimeLeftShownInHours:!0
,notificationEmailId:26,userProfile:{profileImgId:3,width:400,height:400
,quality:100}},fe=a("a18a"),ke=a.n(fe),ve={class:"uk-text-center uk-width
-large uk-align-center"},ye=Object(n["createELementVNode"])("img",{class:"uk
我需要使用 preg_replace 更改我的 css 样式
答:
0赞
mickmackusa
2/1/2023
#1
据我所知,您需要应用模式分隔符和不区分大小写。请参阅模式外部的分隔字符和尾部,以表示正则表达式引擎应使用不区分大小写(因为十六进制颜色代码全大写)。~
i
此外,您应该避免不必要地一遍又一遍地获取和保存相同的内容。相反,获取一次文件内容,执行所有处理,然后保存一次。
代码:(演示)
$settings['new_primary_colour_ccs'] = '#777777'; // test color
foreach ($appPats as $path) {
$styleSettings = [
'primary_colour' => [
'reg' => "~\bprimaryColour:\s?['\"][a-z0-9 ,._+;()'@!?&#:\/-]+['\"]~i",
'string' => 'primaryColour:"' . $settings['new_primary_colour_ccs'] . '"',
'value' => $settings['new_primary_colour_ccs']
]
];
$content = file_get_contents($path);
foreach ($styleSettings as $style) {
$content = preg_replace($style['reg'], $style['string'], $content);
}
file_put_contents($path, $content);
}
如果在实际应用程序中,父循环内没有更改,请将声明移到该循环上方。继续使用相同的数据重新声明它是没有意义的。$styleSettings
评论
preg_replace