php 采集书并合成txt格式的实现代码

yipeiwu_com6年前PHP代码库
<?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>/","\r\n",$arr[6]));
$result = "\r\n------------------------------------------------\r\n------------------------------------------------\r\n------------------------------------------------\r\n----------------".$arr[2]."\r\n------------------------------------------------\r\n------------------------------------------------\r\n------------------------------------------------\r\n".$arr[6];
writer($result, './'.$folder.'/new.txt');
}
?>

相关文章

php之curl实现http与https请求的方法

本文实例讲述了php之curl实现http与https请求的方法,分享给大家供大家参考。具体如下: 通常来说,php的curl函数组可以帮助我们把机器伪装成人的行为来抓取网站,下面来分享...

PHP使用header()输出图片缓存实例

本文实例讲述了PHP使用header()输出图片缓存的方法。分享给大家供大家参考。具体分析如下: 在我们生成验证码时会需要直接输入图片,通常会使用到header("Content-typ...

PHP-CGI进程CPU 100% 与 file_get_contents 函数的关系分析

后来,我通过跟踪发现,这类情况的出现,跟 PHP 的 file_get_contents() 函数有着密切的关系。   大、中型网站中,基于 HTTP 协议的 API 接口调用,是家常便...

PHP中fwrite与file_put_contents性能测试代码

function microtimeFloat() {    list($usec,$sec) = explode(" ", microtime());&n...

Thinkphp框架开发移动端接口(1)

本文实例为大家分享了使用Thinkphp框架开发移动端接口代码,给原生APP提供api接口,具体内容如下 1. 使用TP框架时 放在common文件夹下文件名就叫function.php...