Page 1 of 1

[SOLVED]Regex

Posted: Thu May 27, 2004 12:27 pm
by Steveo31
I'm finally getting the hang of this regex stuff. :)

I am planning on making a CSS parser/changer that will allow the user to change the colors of the page to what they want. Here's what I have for getting the syntax out of the .css file:

Code: Select all

preg_match("/^([a-z]+)?([\{])?([a-z:\-]+)?(.+)(\})+/", $file);
There are a few things this won't match, I know that, I just have to get the basics down before I move on to bigger fish.

This will work on stuff like body{color:FFFFFF;}, but not stuff like body{color:FFFFFF;white-space:normal;}. I don't expect you to do this for me, but you could you suggest some changes I can make to make this regex recognize multiple values like that?

Thanks all. :)

Posted: Thu May 27, 2004 12:31 pm
by Weirdan
ehh....
/color:([^;]*)/
huh?

Posted: Thu May 27, 2004 11:48 pm
by Steveo31
Eh?

Posted: Fri May 28, 2004 2:10 am
by feyd
was screwing around with the idea, here's what I came up with

Code: Select all

<?php

$file = "body
{
	color:#111;
	display:inline;
	filter: progid:DXImageTransform.Microsoft.filtername(sProperties);
}

body2{color:#000;display:inline;filter: progid:DXImageTransform.Microsoft.filtername(sProperties);}

body3{
color:#222;
display:inline;
filter: progid:DXImageTransform.Microsoft.filtername(sProperties);
}

/*

	remove the comments

*/

			body .super #idnum		{

color:
	#333;


display:
inline;



filter: progid:DXImageTransform.Microsoft.Alpha(opacity:50);

}";

//	remove comments
$file = preg_replace("|\s*/\*.*?\*/\s*|s","",$file);

//	remove not needed spaces
$file = preg_replace("|\s*([\{\}:;])\s*|","\\1",$file);

//echo '<pre>'.print_r($file,true).'</pre>';
//	tear the blocks apart
$clean = preg_split("|\s*?[{}]\s*?|",$file,-1,PREG_SPLIT_NO_EMPTY);
//echo '<pre>'.print_r($clean,true).'</pre>';

$x = 0;
$last = "";
foreach($clean as $piece)
{
	if($x % 2 == 0)
	{
		$tag = $piece;
	}
	else
	{
		$tags[$tag] = array();
			$piece = preg_split("|\s*?;\s*?|",$piece,-1,PREG_SPLIT_NO_EMPTY);

		foreach($piece as $part)
		{
			$part = preg_split("|\s*?:\s*?|",$part,2,PREG_SPLIT_NO_EMPTY);
			$tags[$tag][$part[0]] = $part[1];
		}
	}
	$x++;
}
//	spit it out
echo '<pre>'.print_r($tags,true).'</pre>';
}

?>
outputs

Code: Select all

Array
(
    &#1111;body] =&gt; Array
        (
            &#1111;color] =&gt; #111
            &#1111;display] =&gt; inline
            &#1111;filter] =&gt; progid:DXImageTransform.Microsoft.filtername(sProperties)
        )

    &#1111;body2] =&gt; Array
        (
            &#1111;color] =&gt; #000
            &#1111;display] =&gt; inline
            &#1111;filter] =&gt; progid:DXImageTransform.Microsoft.filtername(sProperties)
        )

    &#1111;body3] =&gt; Array
        (
            &#1111;color] =&gt; #222
            &#1111;display] =&gt; inline
            &#1111;filter] =&gt; progid:DXImageTransform.Microsoft.filtername(sProperties)
        )

    &#1111;body .super #idnum] =&gt; Array
        (
            &#1111;color] =&gt; #333
            &#1111;display] =&gt; inline
            &#1111;filter] =&gt; progid:DXImageTransform.Microsoft.Alpha(opacity:50)
        )

)
course, this won't take care of shared definitions.... sorta.

[edit] thinking about it, when showing the tag names, you could split on the comma (with trailing and leading spaces, probably)..

Posted: Fri May 28, 2004 10:20 am
by Steveo31
Wow. Hah.

Um, thanks!