Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php css2array 1

Status
Not open for further replies.

ThomasJSmart

Programmer
Sep 16, 2002
634
Hi

Been trying to googlefind a php class to convert a CSS document into an array. I hav'nt been able to find anything though :/ anybody here know of an existing script, function or class to do this?



exaple:

body,div{
font-size:11px;
color:#000000;
}

.title{
font-size:20px;
color:#990000;
}

would become:

$classArr['body']['font-size'] = '11px';
$classArr['body']['color'] = '#000000';
$classArr['div']['font-size'] = '11px';
$classArr['div']['color'] = '#000000';
$classArr['.title']['font-size'] = '20px';
$classArr['.title']['color'] = '#990000';




I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
well after a bit of fiddeling iv made one that seems to work ok so far :) let me know any improvements ^^




Code:
$sloc = '../../style.css'; 
$sfile = fopen($sloc, 'r'); 
$style = fread($sfile, filesize($sloc)); 
fclose($sfile);


$styles = trim(str_replace("\n",'',str_replace("\r",'',preg_replace('/(\/\*)(.*?)(\*\/)/i','',$styles))));


$s=0; $lastpos=0;
while($i<strlen($styles)){
	
	if($styles{$i}=='{'){
		$key[$s] = substr($styles,$lastpos,$i-$lastpos);
		$lastpos=$i+1;
	}
	
	if($styles{$i}=='}'){
		$val[$s] = substr($styles,$lastpos,$i-$lastpos);
		$lastpos=$i+1;
	}
	
	if(isset($key[$s]) && isset($val[$s])){ $s++;}
	$i++;
}


for($k=0;$k<count($key);$k++){
	$subKey = explode(',',$key[$k]);
	
	for($sk=0;$sk<count($subKey);$sk++){
		$subVal = explode(';',$val[$k]);
			
		for($sv=0;$sv<count($subVal);$sv++){
			$valParts = explode(':',$subVal[$sv]);
			
			if(trim($valParts[1])!=''){
				$classArray[trim($subKey[$sk])][trim($valParts[0])] = trim($valParts[1]);
			}
		}	
		
	}
	
}



echo '<pre>';

print_r($classArray);

echo '</pre>';

I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
this should work
Code:
function css2Array($css){
	$cssArray = array();
	preg_match_all('/(.*?){(.*?)}/isx', $css, $matches);
	foreach ($matches[1] as $key=>$dec){
		$attributes= array();
		preg_match_all('/(.*?):(.*?)(;|$)/isx', $matches[2][$key], $_attributes);
		foreach($_attributes[1] as $i=>$j){
			$attributes[trim($j)] = trim($_attributes[2][$i]);
		}
		$classes = explode(',', $dec);
		foreach($classes as $class){
			$cssArray[trim($class)] = (isset($cssArray[trim($class)])) ? array_merge($cssArray[$class], $attributes) : $attributes; 
		}
	}
	return $cssArray;
}
and will handle the reality that a class can be declared multiple times with different attributes. if attributes are repeated then later will replace earlier (as is true for css itself).

usage example would be
Code:
echo "<pre>";
print_r(css2array(file_get_contents('mycss.css')));
 
ow that is very nice and clean :D
must be great to own regex :p
thank you for this.

I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
no probs!

i've struggled with regular expressions for years. and am only now coming to terms with things like positive/negative lookbehing/ahead etc. it also really annoys me that you can't do matching brackets/quotes easily.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top