0

I want to open a CSS file, search for a known classname and overwrite the old part with a new style

I have this array which contains the name of the class and the new styles

$newstyle['myclassname'][0]='color:red';
$newstyle['myclassname'][1]='another style';
$newstyle['myclassname'][2]='another style';
...

So I create a new style from this

$change=implode(';',$newstyle['myclassname']);

I work on a codeigniter project so I start to open the file

$fp = fopen($path.'myfile.css','wb');

Now I have a idea what I have to do, but I don't know the easiest way.

After open the file, I must search the ".myclassname" or "#myclassname" After that I have to delete or replace the part between {...old styles...} with the new styles which I have to create from the array.

How can I find or replace the old styles with the new ones?

Hope somebody can show me a simple way to to these steps!?

Thanks a lot.

7
  • Is this a one-time thing, or are you trying to build a system that has certain styles customizable via some sort of backend? Because for the latter, that’s not a good approach to begin with, IMHO. That should rather use CSS variables for such a purpose (plus a polyfill, if you need to support IE), or have the stylesheet be a PHP script to begin with, so that you can output the values of PHP variables in the correct places. (The output of such a script could then be cached on disk, if desired.) Commented Mar 8, 2019 at 10:54
  • @04FS it's a part of a small cms Commented Mar 8, 2019 at 10:55
  • @treyBake no dublicate, i not only want to replace one string with another string. I dont know how i can find the part i need and replace that - this is not the question or answear in your suggested other question Commented Mar 8, 2019 at 11:00
  • the suggested answer replaces a string in a file .. just like you want? o.O Commented Mar 8, 2019 at 11:04
  • @DarkBee not sure tbh - but yeah preg_replace is usually the better one for more complicated str_replace's Commented Mar 8, 2019 at 11:06

1 Answer 1

2

You can use a regex pattern to find your class in your file and execute preg_replace() to replace your class CSS:

$css = '.myClass { color:blue; background-color:red; }';
$class = 'myClass';
$classCss = 'color:red;';

$newCss = preg_replace('/(.*(?:\.|#)' . $class . '.*){.*}/s', '$1 { ' . $classCss . ' }', $css);

As we don't really know what your CSS looks like, the code above is a simple example, so if your CSS file have many block with the class that you are searching, it may not work as intended and you may have to adapt the regex pattern.

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

6 Comments

oh ok, by using preg_replace should be the way. Is there a way to check if the class we are looking for is inside the file and if not, add them instead replace?
preg_match() returns true if the specified pattern is found in the subject. If that's the case it is a replacement. Else, you can add.
:-) top - do you know a smarter way to check the css.file instead fopen or file_get_contents?
Personally I would use file_get_contents. It may have a better way but I don't it yet if that's the case! Better post a new question to have your answer!
after testing your code, it seems that he no return false, it seems that he do everytime anything!? By testing if the class is not inside the file, he returns true instead false
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.