如何使用PHP4解析HTML
问题描述:
我正在只有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是这种类型的任务的选项?
答
它可以使用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>
。
答
看起来你使用的语法是PHP Simple HTML DOM Parser,它只支持PHP 5+。
根据该网站,该包装的灵感来自Jose Solorzano的HTML Parser for PHP 4。所以这可能有助于你检查出来。
检查此问题:http://*.com/questions/3503276/porting-php5-to-legacy-php4-domdocument-quibbles – Brad 2012-08-04 01:18:39
可能是[php4-html-dom](http:///php4-html-dom.sourceforge.net/documentation/php4-html-dom/htmlParser.html)? – elias 2012-08-04 01:20:40
@brad谢谢,但我是新的PHP解析。也许你可以写一个关于如何在php4服务器上使用php5代码的答案 – user176105 2012-08-04 01:21:57