Page 1 of 1

modifying parts of a css file via php

Posted: Wed Oct 26, 2005 12:58 pm
by Dark[NSF]
In my php script, I am parsing through some css code. It seperates out the classes and variables to form form text fields and whatnot. What I'd like to do is be able to modify the variables, so I was thinking...


Ok, I'll just have it search for that exact original variable (ie.. background-color: black;) and replace it with what i changed it to for example (ie background-color: red;). Then I realized that more than one class could have background-color: black.

So I guess my question is, how would I pick out and change a specific variable in a css file?





If this is unclear or you need some source, please ask. thanks in advance. :wink:

Posted: Wed Oct 26, 2005 1:09 pm
by Chris Corbyn
Either:

a) Use Regular Expressions (Regex) to find the correct parts in the CSS

or

b) Mix the CSS and PHP directly, so that PHP can write directly to the CSS code

I prefer the latter, which can be done in two different ways:

Method one, via a stylesheet written in PHP:

Code: Select all

<html>
<head>
<style type="text/css" media="screen">
@import "/path/to/phpscript.php?arguments=values"
default-css-attributes: default-values;
</style>
</head>
<body>
....
and the php file:

Code: Select all

<?php

header('Content-type: text/css');

if (!empty($_GET['some_var']))
{
    $somevar = $_GET['some_var'];
}

?>
div#an_example_id
{
    background-color: <?= $somevar; ?>;
    height: 20px;
}
Alternatively you can just include all that CSS in the <style> tag itself ;)