php 采集书并合成txt格式的实现代码
程序员文章站
2022-05-30 13:05:47
<?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');
}
?>
/**
* @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');
}
?>
上一篇: 把袋子套脑袋上
下一篇: 国际厂商发力 2010年音箱行业年终盘点