Jump to content

Recommended Posts

In this example i'm scanning a css file for colour hash codes...

 

$match='/(#[a-f0-9]{3,6})/is';	//	matches colour hashes from 3 to 6 in length
preg_match_all($match, $s, $a, PREG_PATTERN_ORDER);
That works ok for now, what i'm after is how once matched as above how to get some text from before the mmatch returned as a seperate group.

 

Example css:

body {

background: #fff;

}

 

a {

color: #000;

text-decoration: none;

}

At the moment i'm just returned "#fff" and "#000", what i'm after is ("#fff","body") and ("#000","a"). So basically after match come back to before "{" and either to start of file or next "}".

 

Any help please?

I understand that you like regexes and want to use them for this task, but they're simply not the right tool. Not even by the lowest standards.

 

Comments are just one “anomaly”. You may also run into ID selectors or URL fragments or whatever. The whole thing is basically a shot in the dark. And since you've made the task even more complex, I fearly you'll quickly end up with a complete mess of buggy, unreadable regexes which nobody can possibly maintain. For what?

 

CSS is an actual language, it has a very specific structure. So what you should do is grab a CSS parser (there are plenty) and make it build a syntax tree. This gives you all the data you need in the right context.

It could be more adaptive or even maybe written in a one liner, but here's my quick hack...

 

<?php
//	Simple CSS Parser
//	www.rawstar7.co.uk/site

$fn="tmp/style.css";
$f = fopen($fn, "rb");
$contents = fread($f, filesize($fn));
fclose($f);

$a=css_parse($contents);
$out=css_print($a);

function css_parse($s){
	
	//	STRIP OUT COMMENTS (Can be done elsewhere or missed out completely!)
	$s = preg_replace('!/\*.*?\*/!s', '', $s);
	$s = preg_replace('/\n\s*\n/', "\n", $s);
	
	//	SPLIT BY "}"
	$a=explode("}",$s);
	$acss=array();
	foreach($a as $e){
		//	SPLIT BY "{"
		$aa=explode("{",$e);
		
		//	SPLIT NAMES LIST
		$anames=preg_split("/[\s,]+/", trim($aa[0]));
		
		//	SPLIT TAGS LIST
		$atags=explode(";",$aa[1]);
		
		//	SPLIT INDIVIDUAL TAGS INTWO K => V PAIRS
		
		//	BUNDLE IT UP 
		$acss[]=array($anames,$atags);
	}
	return $acss;
}

function css_print($a){
	$sret= "<table>";
	foreach($a as $e){
		$sret.= "<tr><td>";
		foreach($e[0] as $ee){
			$sret.= $ee."<br />";
		}
		$sret.= "</td><td>";
		foreach($e[1] as $ee){
			$sret.= $ee."<br />";
		}
		$sret.= "</td></tr>\n";
	}
	$sret.= "</table>";
	
	return $sret;
}
?>

<html><head>
<style> table,th,td {	border: 1px solid #000; 	vertical-align: text-top;	} </style>
</head><body>
<?php echo $out; ?>
</body></html>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.