-2
$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

7
  • Start with a valid PCRE regular expression at least? You current attempt should only result in "Warning: preg_replace(): Delimiter must not be alphanumeric, backslash, or NUL" Commented Jan 31, 2023 at 12:02
  • Yes I tested it, regular expression is correct Commented Jan 31, 2023 at 12:36
  • The regular expression itself might be correct - but what you are feeding to preg_replace as first parameter, is not. (If you are really stumbling around blind without proper PHP error reporting enabled, so that PHP did not tell you what is wrong, then please go and enable that first of all now.) Commented Jan 31, 2023 at 12:38
  • Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in ... Commented Jan 31, 2023 at 12:43
  • If you don't know what this error means, please research it. Not a new topic/issue, not by a long shot. Commented Jan 31, 2023 at 12:48

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.