2014-06-29 22 views
3

我正在使用Php Html Dom Parser來獲取元素。但它沒有得到和內部文本元素。請參閱下面的代碼;Php Html Dom Parser沒有得到<style>和<script>元素

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

我有這種類型的網頁上的CSS代碼

<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> 

,我想每元素與內部文本。

感謝

回答

4

你已經在瞄準style標籤正確。但是您需要使用->innertext magic屬性來獲取值。考慮下面這個例子:

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應該輸出:

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;} 
) 
+1

太好了!它正在工作。謝謝:) –

+1

@SmacAfzal沒有概率兄弟 – user1978142