I am trying to extract colors from a css file using php.
These colors may be:

1. normal colors (color: #xxxxxx;)
2. background colors (background: #xxxxxx; / background: ... #xxxxxx ...;)
3. background gradients (background-image: linear-gradient(top, #xxxxxx, #xxxxxx);
4. sometimes colors may be 3 chars (eg: #fff)

I tried preg match to return words starting with #

```
preg_match_all('/(?!\b)(#\w+\b)/', $css_text, $matches)

```

...but it also returns DIV Ids (#header etc.)

Moving forward, I also wish our code to return a multidimensional array with not only color codes but also line number on which it was found.

Please help! :)

---------- EDIT: Question SOLVED ----------

Thankyou all for you answers, I kind of combined everybody's answers.
As i wanted to keep regex to minimum, **this is what i used as final working code**:

```
$css = file_get_contents("style.css");

$token = strtok($css, "{}");
$css_parts = array();
while ($token !== false) {
    $css_parts[] = trim($token);
    $token = strtok("{}");
}

$flag = false; $properties = "";
    foreach($css_parts as $part) {
    if($flag) { $properties .= " ".trim($part); }
    $flag = !$flag;
}
$properties = strtoupper(str_replace(array(":",",",";","(",")")," ",$properties));

$colors = array();
preg_match_all('/(?!\b)(#\w+\b)/',$properties,$colors);
$colors = array_unique($colors[0]);

print_r($colors);

```