Reputation: 9416
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
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
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