M K
M K

Reputation: 9416

Extract css from html

I'm looking for a clean way to grab and remove all the css between the <style></style> tags. For example:

<style>
foo
</style>

content

<style>
bar
</style>

here

By the end of the process I should have a string with content\nhere (everything not between the style tags) and an array of matches between the style tags ['foo', 'bar'] or something similar. I've tried a lot of different regex approaches and none of the seemed to work for me. (I'm no regex pro though..)

Upvotes: 1

Views: 694

Answers (2)

Marcel
Marcel

Reputation: 5119

You should not access html with regular expressions. Just try DomDocument instead. It 's more cleaner and easier to access.

$dom = new DomDocument();
$dom->loadHTML('Your HTML here');

$elements = $dom->getElementsByTagName('style');
for ($i = $elements->length; --$i >= 0;) {
    $elements->item($i)->parentNode->removeChild($elements->item($i));
}

echo $dom->saveHTML();

This code is an example and not tested.

Upvotes: 6

M K
M K

Reputation: 9416

I found the answer:

$html = "<style>css css css</style>my html<style>more css</style>";
preg_match_all("/<style>(.*?)<\/style>/is", $html, $matches);

$html = str_replace($matches[0], '', $html);
$css = implode("\n", $matches[1]);

echo $html; // my html
echi $css; // css css css \n more css

Originally I was looking for a pure regex solution, but this is fine for me.

Upvotes: 1

Related Questions