php 采集书并合成txt格式的实现代码
作者:bea
<?php /** * @name 采集书.php * @date Sun Mar 01 22:48:02 CST 2009 * @copyright 马永占(MyZ) * @author 马永占(MyZ) * @link http://blog.csdn.net/mayongzhan/ */ //header('Content-Type:text/html;charset=utf8'); header('Content-Type:text/html;charset=g
<?php
/**
* @name 采集书.php
* @date Sun Mar 01 22:48:02 CST 2009
* @copyright 马永占(MyZ)
* @author 马永占(MyZ)
* @link http://blog.csdn.net/mayongzhan/
*/
//header('Content-Type:text/html;charset=utf8');
header('Content-Type:text/html;charset=gb2312');
error_reporting(E_ALL);
date_default_timezone_set('Asia/Shanghai');
set_time_limit(0);
function writer($content,$url)
{
$fp = fopen($url, 'ab');
fwrite($fp, $content);
fclose($fp);
}
$folder = '2'; //文件夹
$book_base_url = 'xxxxxxxxxxxxxxxxxxxxx';
$book_url = 'yyyyyyyyyyyyy.html';
$main = file_get_contents($book_base_url.$book_url);
preg_match_all('/chapter_.*?.html/', $main, $pages);
$pages = array_unique($pages[0]);
foreach ($pages as $value) {
writer(file_get_contents($book_base_url.$value), './'.$folder.'/'.$value.'.txt');
$str = file_get_contents('./'.$folder.'/'.$value.'.txt');
//print_r($str);
preg_match("/(<h1>)(.*?)(</h1>)(.*?)(<div id="contTxt" class="contTxt1">)(.*?)(</div>)/s",$str,$arr);
//print_r($arr);die();
$arr[6] = preg_replace("/(<span[^>]+>.*?<a[^>]+>)(.*?)(</a></span>)/s","$2",preg_replace("/<p>|</p>/","
",$arr[6]));
$result = "
------------------------------------------------
------------------------------------------------
------------------------------------------------
----------------".$arr[2]."
------------------------------------------------
------------------------------------------------
------------------------------------------------
".$arr[6];
writer($result, './'.$folder.'/new.txt');
}
?>
有用 | 无用
/**
* @name 采集书.php
* @date Sun Mar 01 22:48:02 CST 2009
* @copyright 马永占(MyZ)
* @author 马永占(MyZ)
* @link http://blog.csdn.net/mayongzhan/
*/
//header('Content-Type:text/html;charset=utf8');
header('Content-Type:text/html;charset=gb2312');
error_reporting(E_ALL);
date_default_timezone_set('Asia/Shanghai');
set_time_limit(0);
function writer($content,$url)
{
$fp = fopen($url, 'ab');
fwrite($fp, $content);
fclose($fp);
}
$folder = '2'; //文件夹
$book_base_url = 'xxxxxxxxxxxxxxxxxxxxx';
$book_url = 'yyyyyyyyyyyyy.html';
$main = file_get_contents($book_base_url.$book_url);
preg_match_all('/chapter_.*?.html/', $main, $pages);
$pages = array_unique($pages[0]);
foreach ($pages as $value) {
writer(file_get_contents($book_base_url.$value), './'.$folder.'/'.$value.'.txt');
$str = file_get_contents('./'.$folder.'/'.$value.'.txt');
//print_r($str);
preg_match("/(<h1>)(.*?)(</h1>)(.*?)(<div id="contTxt" class="contTxt1">)(.*?)(</div>)/s",$str,$arr);
//print_r($arr);die();
$arr[6] = preg_replace("/(<span[^>]+>.*?<a[^>]+>)(.*?)(</a></span>)/s","$2",preg_replace("/<p>|</p>/","
",$arr[6]));
$result = "
------------------------------------------------
------------------------------------------------
------------------------------------------------
----------------".$arr[2]."
------------------------------------------------
------------------------------------------------
------------------------------------------------
".$arr[6];
writer($result, './'.$folder.'/new.txt');
}
?>
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- php 表单数据的获取代码
- php URL编码解码函数代码
- php 进度条实现代码
- php 表单验证实现代码
- php连接mysql数据库代码
- php执行sql语句的写法
- php 结果集的分页实现代码
- php 动态添加记录
- PHP中查询SQL Server或Sybase时TEXT字段被截断的解决方法
- php mssql 日期出现中文字符的解决方法
- linux php mysql数据库备份实现代码
- php生成SessionID和图片校验码的思路和实现代码
- php 数组的创建、调用和更新实现代码
- 在JavaScript中调用php程序
- 隐性调用php程序的方法
- php动态生成JavaScript代码
- Zend 输出产生XML解析错误
- 解决了Ajax、MySQL 和 Zend Framework 的乱码问题
- php Try Catch异常测试