RegEX CSS

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Hi,

    ich hab mal wieder ein RegEx Problem, welches mich jetzt schon immens viel Zeit kostet.
    Ich hoffe ihr könnt mir helfen.

    Ich möchte aus einem CSS File per PHP auslesen und dann bestimmte CSS IDs inklusive Eigenschaften in ein Array packen.
    Das Problem ist nun, ich bekomm die Eigenschaften nur in einem Array und nicht getrennt.
    Hier mein Beispiel

    CSS-Quellcode

    1. #plugin_task{
    2. background-image: url([url]http://xxx/images/taskicon_pluginbar.png[/url]);
    3. background-repeat: no-repeat;
    4. }

    Quellcode

    1. #^#(plugin_task)[\s|\n]*{[\n|\s]*(.*\:.*;[\n|\s]*)*.*}$#


    Liefert:

    Quellcode

    1. Array
    2. (
    3. [0] => Array
    4. (
    5. [0] => #plugin_task{
    6. background-image: url([url]http://xxx/images/taskicon_pluginbar.png[/url]);
    7. background-repeat: no-repeat;
    8. }
    9. )
    10. [1] => Array
    11. (
    12. [0] => plugin_task
    13. )
    14. [2] => Array
    15. (
    16. [0] => background-image: url([url]http://xxx/images/taskicon_pluginbar.png[/url]);
    17. background-repeat: no-repeat;
    18. )
    19. )
    Alles anzeigen


    Ich hätte aber gern:

    Quellcode

    1. Array
    2. (
    3. [0] => Array
    4. (
    5. [0] => #plugin_task{
    6. background-image: url([url]http://xxx/images/taskicon_pluginbar.png[/url]);
    7. background-repeat: no-repeat;
    8. }
    9. )
    10. [1] => Array
    11. (
    12. [0] => plugin_task
    13. )
    14. [2] => Array
    15. (
    16. [0] => background-image: url([url]http://xxx/images/taskicon_pluginbar.png[/url]);
    17. )
    18. [3] => Array
    19. (
    20. [0] => background-repeat: no-repeat;
    21. )
    22. )
    Alles anzeigen



    Vielleicht kann einer helfen :)
  • ich muss ganz ehrlich sagen, dass ich dein gewünschtes Format nicht gut finde. Was willst du denn mit einem so unnormalisierten Array?

    ich habe mal schnell folgenden CSS Parser geschrieben:

    Quellcode

    1. $css = file_get_contents('dateiname.css');
    2. preg_match_all('/([^\{]+)\{([^\}]+)\}/siU', $css, $res);
    3. $keys = array_map("trim", $res[1]);
    4. $vals = array_map(create_function('$a', 'return array_filter(array_map("trim", explode(";", $a)));'), $res[2]);
    5. $return = array();
    6. for($i=0; $i<count($keys); $i++) {
    7. $return[] = array(
    8. $keys[$i],
    9. $vals[$i]
    10. );
    11. }
    12. print_r($return);
    Alles anzeigen


    Zielformat:

    Quellcode

    1. [0] => Array
    2. (
    3. [0] => #plugin_task
    4. [1] => Array
    5. (
    6. [0] => background-image: url(http://xxx/images/taskicon_pluginbar.png)
    7. [1] => background-repeat: no-repeat
    8. )
    9. )


    Ansonsten such mal im Internet nach richtigen CSS Parsern, da gibts auch eine menge mechtiger Varianten: google.com/codesearch/p?hl=de#…0lang:php&sa=N&cd=6&ct=rc