Home > other >  Replace portion of css file content using preg_replace()
Replace portion of css file content using preg_replace()

Time:02-01

$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);
    }
}

My css text looks like this:

(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

I need to change my css style using preg_replace

CodePudding user response:

From what I see, you need to apply pattern delimiters and case-insensitivity. See the ~ delimiting characters on the outside of the pattern and the trailing i to signify that case-insensitivity should be used by the regex engine (because the hex color code is in all-caps).

Also, you should avoid needlessly fetching and saving the same content over and over. Instead, fetch the file content once, do all the processing, then save it once.

Code: (Demo)

$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);
}

If in your actual application, $styleSettings doesn't change inside the parent loop, move the declaration above that loop. It doesn't make sense to keep re-declaring it with the same data.

  • Related