[SOLVED]Regex

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

[SOLVED]Regex

Post 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. :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

ehh....
/color:([^;]*)/
huh?
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Eh?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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)..
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Wow. Hah.

Um, thanks!
Post Reply