Saeed Afzal
Saeed Afzal

Reputation: 368

Php Html Dom Parser not getting <style> and <script> elements

I'm using Php Html Dom Parser to get the elements. But it's not getting and element with inner text. See the code below;

$html = file_get_html($currentFile);
foreach($html->find('style') as $e){
  echo $e->plaintext;
}

I have this type of on page CSS code

<style type="text/css">
ul.gallery li.none { display:none;}
ul.gallery { margin:35px 24px 0 19px;}
</style>
<!--<![endif]-->
<style type="text/css">
body { background:#FFF url(images/bg.gif) repeat-x;}
</style>

and I want to get each and element with inner text.

Thanks

Upvotes: 4

Views: 2758

Answers (1)

user1978142
user1978142

Reputation: 7948

You're already correct in targeting the style tag. But you need to use the ->innertext magic attribute to get the values. Consider this example:

include 'simple_html_dom.php';
$html_string = '<style type="text/css">ul.gallery li.none { display:none;}ul.gallery { margin:35px 24px 0 19px;}</style><!--<![endif]--><style type="text/css">body { background:#FFF url(images/bg.gif) repeat-x;}</style>';
$html = str_get_html($html_string); // or file_get_html in your case
$styles = array();
foreach($html->find('style') as $style) {
    $styles[] = $style->innertext;
}

echo '<pre>';
print_r($styles);

$styles should output:

Array
(
    [0] => ul.gallery li.none { display:none;}ul.gallery { margin:35px 24px 0 19px;}
    [1] => body { background:#FFF url(images/bg.gif) repeat-x;}
)

Upvotes: 5

Related Questions