Bolboa Bolboa - 1 year ago 78
PHP Question

php regex to get strings that start with any character and end with open brace

I have css file that I am trying to manipulate using php. So if my css is like...

.something {
background: blue;

.somethingElse {

I want to be able to get an array of the class names. So my array would look like...

['.something', '.somethingElse']

This is my attempt (
is my css file)...

$homepage = file_get_contents("style.css");
$regex = '/[\s\S]\K[^{]*(?=})/m';
preg_match_all($regex, $homepage, $matches);

What I tried to do was find all strings that start with any character and end in open brace
. My regex is all wrong, what is the proper way?

Answer Source

This should work great:


The class name is in group #1.


preg_match_all("/\.([\w]*)\s*{/", $homepage, $matches);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download