2012-08-04 46 views
1

我正在只有PHP4的服務器上編寫站點。我正在嘗試解析我學校的網站以獲取課程會議時間和地點(this website)。不,我無法讓管理員安裝PHP5。這是我的代碼,返回一個錯誤:如何使用PHP4解析HTML

// Create DOM from URL or file 
$html = file_get_html('http://www.google.com/'); 

// Find all images 
foreach($html->find('img') as $element) 
    echo $element->src . '<br>'; 

JavaScript是這種類型的任務的選項?

+1

檢查此問題:http://stackoverflow.com/questions/3503276/porting-php5-to-legacy-php4-domdocument-quibbles – Brad 2012-08-04 01:18:39

+1

可能是[php4-html-dom](http:///php4-html-dom.sourceforge.net/documentation/php4-html-dom/htmlParser.html)? – elias 2012-08-04 01:20:40

+0

@brad謝謝,但我是新的PHP解析。也許你可以寫一個關於如何在php4服務器上使用php5代碼的答案 – user176105 2012-08-04 01:21:57

回答

0

它可以使用jQuery來完成:

1.Get頁面用PHP 2.打印它在div

<?php 
echo "<div id='imgSrc'><ul></ul></div>"; 
echo "<div id='scrapperContent' style='display:none'>"; 
echo file_get_contents($url); 
echo "</div>"; 
?> 

內容現在添加PHP代碼上面的下一個代碼:

<script> 
$(document).ready(function() { 
    $("img").each(function (i) { 
     $('#imgSrc ul').append("<li>" + $(this).attr("src") + "</li>"); 
     }); 
}); 
</script> 

正如你所看到的,它的工作原理:http://jsfiddle.net/Sv33T/

但是,我剛剛注意到,學校網站中根本沒有使用<img>