modifying parts of a css file via php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dark[NSF]
Forum Newbie
Posts: 18
Joined: Thu Oct 06, 2005 9:25 am
Location: Palm Bay, FL
Contact:

modifying parts of a css file via php

Post 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:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
Post Reply