Bolboa Bolboa - 1 month ago 12
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 {
display:none;
background: blue;
}

.somethingElse {
display:block;
}


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 (
$homepage
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

This should work great:

\.([\w]*)\s*{

The class name is in group #1.

Example:

preg_match_all("/\.([\w]*)\s*{/", $homepage, $matches);
Comments