当前位置: 首页 > 图文教程 > 网络编程 > PHP > 也谈截取首页新闻 - 范例

PHP
PHP实例教程:Output Control输出函数
memcached和mysql主从环境下PHP开发
基于LAMP架构设计的WEB框架
PHP代码:验证IPV6地址是否合法的正则
PHP环境快读搭建绿色软件包PHPnow
PHP教程:$_SERVER的详细参数整理
php获取url字符串截取路径的文件名和扩展名的函数
在命令行下运行PHP脚本[带参数]的方法
PHP 实用代码收集
PHP 时间转换Unix时间戳代码
关于php fread()使用技巧
PHPMailer 中文使用说明小结
php addslashes和mysql_real_escape_string
php cout<<的一点看法
PHP 变量的定义方法
php学习之 认清变量的作用范围
php 静态变量与自定义常量的使用方法
认识并使用PHP超级全局变量
通过具体程序来理解PHP里面的抽象类
php读取xml实例代码

PHP 中的 也谈截取首页新闻 - 范例


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 216 ::
收藏到网摘: n/a

截取yahoo.com.cn新闻[仅供实验]
有很多截取首页新闻的程序,但是并不能成功。
他们的工作原理无非两种,一种是采用某些网站称之为backend的后端数据库接口,另一种则是硬声声的根据html代码截取。本程序采用的是后者。应该说,容错性能比较好。
<?
$open = fopen("http://www.yahoo.com.cn/index.html", "r");//网页地址
$read = fread($open, 15000);
fclose($open);
$search = eregi("<!-- Start in the news -->(.*)<!-- End in the news -->", $read, $printing);//截取一段源代码,最好先分析一下源代码
//以下开始取出容余源代码
$printing[1] = str_replace("href=\"/homer/?", "href=\"", $printing[1]);
$printing[1] = str_replace("href=\"/headlines/fullcoverage/", "href=\"http://www.yahoo.com.cn/headlines/fullcoverage/", $printing[1]);
$printing[1] = str_replace("</td></tr><tr><td valign=top align=right>", "", $printing[1]);
$printing[1] = str_replace("</td><td>", "", $printing[1]);
$printing[1] = str_replace(" class=sbody", "", $printing[1]);
$printing[1] = str_replace("</small>", "", $printing[1]);
$content = $printing[1];
$content = explode("-", $content);
$headlines = sizeof($content);
for ($i = 0; $i < $headlines; $i++) {
print "新闻".($i+1).") : $content[$i]<BR>";//激动人心的时刻到了!显示出来了!
}
?>

在php3/php4 apache下调试通过。