0

I'm building the backend to a web application, where it is possible to change the design of a page. Once the user has setup the design the way they like it, they should be able to save it and the new design would be written to the specific file.

For this reason, I will need to replace all the characters between { and } after a certain string, which would be the name of the class.

So a very simple concept, say the following class was in a seperate file which I load as a view, style.php. I would need to go from:

from

<style>
.bonus {
    bottom: 6px;
    left: 247px;
}
</style>

to

<style>
.bonus {
    bottom: 20px;
    left: 450px;
}
</style>

Could someone recommend me on the best way to

a) find a string in a file,

b) when that is found, replace everything between two strings right after the first string.

Thank you.

4
  • I wouldn't do a find and replace, I would have your backend generate a new CSS stylesheet which is called after your main stylesheet. Commented Jul 23, 2013 at 18:19
  • Actually, that sounds like a much better idea. But just for the sake of knowing I will leave this question open. Commented Jul 23, 2013 at 18:20
  • You can use a PHP CSS Parser, manipulate the structure it gives then output a new CSS file after your modifications. Commented Jul 23, 2013 at 18:21
  • 3
    You could also look into server-side CSS languages like LESS and SCSS, where you could have the changing parts as variables. PHP versions of both are available here: lessphp, scssphp (there may be other PHP implementations too, I'm not sure). Commented Jul 23, 2013 at 18:22

2 Answers 2

1

I don't like the concept of the user making changes to the actual file very much. There are a lot of safer methods by which a user could create and maintain a custom template without them actually making changes to a PHP file.

What about storing the user's CSS in a field in a database? Then you'd simply need to do something like:

<?php

     $css = getCSSByUserId($userId); //function runs query on DB to get user-specific CSS

/* $css = ".bonus {
    bottom: 20px;
    left: 450px;
}" */

?>
<style>
     <?php echo $css; ?>
</style>
Sign up to request clarification or add additional context in comments.

1 Comment

Well in this case the only user who can make changes will be the admin, so I believe its permissible.
0

If you really want to edit the actual file, you'd do it something like this:

<?

$file = "/path/to/file.php";

//The user's replacement CSS
 $replace = '.bonus {
    bottom: 20px;
    left: 450px;
}';

$str = file_get_contents($file);

$str = preg_replace('/\.bonus \{.*\}/U', $replace, $str);

$res = fopen($file, 'w');
fwrite($res, $str);
fclose($res);

?>

I checked the regex here http://www.quanetic.com/Regex and it works.

1 Comment

@styke Have your tried this? I wouldn't mind the rep if it works.

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.