4

I retrieve with PHP a css file in my database.

So i have : $mycss = "SOME CSS SOME CSS SOME CSS"

In this $mycss, i can have :

div.coul01,.red,.coul01{
    color:#663333;
}
div.coul02,.blue,.coul02{
    color:#3366CC;
}

For this example, i need to retrive in PHP, for each instruction that begins with div. the second element, here "red" and "blue".

Do you have any ideas ? Thanks !

3
  • A google search reveals github.com/sabberworm/PHP-CSS-Parser . I have not used it though. Commented Jan 25, 2013 at 10:19
  • you want instruction begins with div only ? Commented Jan 25, 2013 at 10:23
  • I can have div., a., form. etc.... Commented Jan 25, 2013 at 10:34

3 Answers 3

1

If you really don't want to use any library and want to stick to regular expressions, this will work for you:

<?php
$mycss = "body,div.coul01,.white,.coul01{
    color:#000000;
}
div.coul01,.red,.coul01{
    color:#663333;
}
div.coul02,.blue,.coul02{
    color:#3366CC;
}
div.coul02,.grey,.coul02{
    color:#C0C0C0;
}";
preg_match_all("/^div\.[^,{]*,[\.#](\w+)/ms", $mycss, $matches, PREG_PATTERN_ORDER, 0);
?>

and then in your $matches variable you will have something like this:

$matches[0][0]  div.coul01,.red
$matches[0][1]  div.coul02,.blue
$matches[0][2]  div.coul02,.grey
$matches[1][0]  red
$matches[1][1]  blue
$matches[1][2]  grey

$matches[1] is what you are looking for

Example

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

5 Comments

i don't want to use any librany neither^^ I test your solution.
@ClémentAndraud I tested it here: xrg.es/#do1xhl and it work only with red and blue like you said
It's an example, i don't know what i have in my css.... If i know red, blue etc.. i don't need a script
In your example i need to retrieve white and grey too. But i can't know what colors i have ;)
@ClémentAndraud you should edit your question with that information, it's not clear enough.. but if you don't know what colours you have, how can you search for them, I mean, darkgrey is a colour? or is it dark-grey? on the other hand if your css were like div.coul01, .color-blue or .color-red is much easier to search
0

You could use explode() with the comma as the separator, but a more elegant solution would be to use a PHP CSS parser, such as https://github.com/sabberworm/PHP-CSS-Parser

It's more flexible.

Comments

0

I would suggest using a CSS parser such as Sabberworm. You could also try this library, which I've seen a few people recommend. There is also a PEAR package that you might want to look at called HTML_CSS. Note that it is currently not being maintained. Personally, I'd stay far away from writing custom regex expressions for this kind of thing, simply because I've seen projects like this "take on wings" and lead to messy regex-riddled code that is difficult to maintain as the project expands and more features are added. One minute you're wanting everything that starts with div, the next you're wanting to control h1, span and p styles.

Comments

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.