Victory Road Archive

You are viewing an archive of Victory Road.

Victory Road closed on January 8, 2018. Thank you for making us a part of your lives since 2006! Please read this thread for details if you missed it.

Technology → Parsing a CSS File with PHP

Page 1 of 1

1. FreezeWarp said on June 25, 2010, 05:41:36 PM (-07:00)

Kyurem
2,186 posts

During development of VictoryBattles, I came across a major problem: VBulletin stores its themes in very complex ways, but does keep its style sheet static in a separate cell. I found that it would be too complicated to use the SQL, so instead I used the CSS, interpreted it, and reprinted it as such. After doing so, I was able to properly render things.

The function I wrote to do this is:

PHP Code:
function parseStyles($css,$winLineEndings false) {
  if (
$winLineEndings) {
    
$end "\r\n";
  }
  else {
    
$end "\n";
  }
  
$style str_replace(array($end,"\t"),'',$css);
  
$style preg_replace('/\<style(.*?)\>(.*?)\<\/style\>/is','$2',$style);
  
$style preg_replace('/\<link(.+?)\/\>/is','',$style);
  
$style preg_replace('/\/\*(.*?)\*\//is','',$style);showing me how

  $values 
= array();
  
preg_match_all('/(.+?)[\s\n]?\{[\s\t\n]?(.+?)[\s\n]?\}/'$style$matches);
  foreach(
$matches[0] AS $i=>$original) {
    foreach(
explode(';'$matches[2][$i]) AS $attr) {
      
// Allows missing end semicolon.
      
if (strlen($attr) > 0) {
        list(
$name$value) = explode(': '$attr);
        foreach(
explode(','$matches[1][$i]) AS $attr2) {
          
$results[trim($attr2)][trim($name)] = trim($value);
          
$results[trim($attr2)][trim($name)] = trim($value);
        }
      }
    }
  }
  return 
$results;

To call the function:
PHP Code:
$style = @mysql_fetch_assoc(@mysql_query('SELECT css FROM style WHERE styleid = ' $user['styleid']));
$style $style['css'];
$data parseStyles($style,true); 
Hat tip to Nathan on stack overflow for the original solution: http://stackoverflow.com/questions/1...array-with-php

2. KingOfKYA said on June 27, 2010, 10:23:30 PM (-07:00)

Volcarona
523 posts

May we will use this for next chat release much better than my static way.(i didn't know where vb kept the styles.) Besides you codes a little more elegant. I relly don't know reg expressions good thing I got a perl class coming up.

3. FreezeWarp said on June 27, 2010, 11:32:36 PM (-07:00)

Kyurem
2,186 posts

Quote:
Originally Posted by KingOfKYA View Post
May we will use this for next chat release much better than my static way.(i didn't know where vb kept the styles.) Besides you codes a little more elegant. I relly don't know reg expressions good thing I got a perl class coming up.
Sure, though I wouldn't mind getting a little more information into that =P

And Perl was my very first programming language (I was sitting in to a $1,000/day class), so it comes natural to me =D

Page 1 of 1

User List - Contact - Privacy Statement - Lycanroc.Net