On Tue, 24 Aug 2004 23:53:53 -0700, Daniel Lahey <[EMAIL PROTECTED]> wrote:
> I'm trying to figure out how to formulate a regular expression that
> will get me everything following a pound sign (#) up to the first space
> or { character.  (I'm trying to parse the ids out of a style sheet.)
> Can anyone point me in the right direction?  I've been searching on the
> web for hours and reading everything I can find on regular expressions,
> but I just can't wrap my brain around it.  Thanks.
> 


Try this snippet
----

<pre>
<?php
$css = fopen("http://www.csszengarden.com//001/001.css";, "r");
$data = fread($css, 30000);
preg_match_all("/#.+({ )/", $data, $out);
print_r($out);
?>
</pre>

It gets the default css from the zengarden site. (30k is a large
buffer, but this is not a howto on buffered reads)

The regular expression 

/#.+({ )/

means, piece by piece


#      the # character
.      dot is a special character in regular expressions. it can match
any character
+      is also a special character,  it means match 1 or more times

so .+ means match any character 1 or more times

( )     are special characters, they match any single character inside them.

in the case of ({ )  , it matches either a space or a left bracket

So, the whole expression means:

match a strings where they start with #, followed by 1 or more
characters, followed by a space or a left-bracket

Note that these are very simplified explanations for regular expressions. 

Now, try the expression

/#(.+)({ )/

and instead of  print_r($out), try

print_r($out[1]);


Now you get the classnames :)  Check out the following sites for more
info (they appear first when you google regular expressions).

http://sitescooper.org/tao_regexps.html
http://etext.lib.virginia.edu/helpsheets/regex.html


Also, check out the PHP manual for the preg_* functions

---
Note that the script i gave you has problems with classnames on one line, like

#footer a:link, #footer a:visited 

But I won't spoon feed you :)

----

ramil

http://ramil.sagum.net

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to