<?php
require 'init.php';
/**
$c1 = '第三百三十八章 变态的青玉将军';
$c2 = '第三百三十八章 变态的青玉将军 T';

echo filterChapterName($c1) . "\n" . filterChapterName($c2) . "\n";
die;
/**/
//$updateList = call_user_func_array(array('Basank', 'getNewList'), array());
//print_r($updateList);die;
//print_r(Site_Kenwen::getNewList());die;
//print_r(\collect\Spider::search('Shenmaxiaoshuo', '绝对秘技', '心上雪'));die;
$spider = Spider::factory('http://www.kenwen.com/detail-32146.html', 'Kenwen');
//print_r($spider);die;
//echo $spider->getChapterListUrl();
$spider->getInfoFile();
/**
echo $spider->getBookName();
echo "\n";
echo $spider->getAuthor();
echo "\n";
echo $spider->getBookInfo();
echo "\n";
echo $spider->getType();
//echo "\n";
echo $spider->getStatus();
echo "\n";
$cover = $spider->getCoverUrl();
var_dump($cover);die;
/**/
/**
$list = $spider->getChapterList();
print_r($list);;die;
/**/
//print_r($spider->clearVolume($list));

/**/
$url = $spider->getChapterContentUrl('4510300.html');
//$url = 'http://www.kenwen.com/cview/32/32146/4451644.html';
//echo $url;die;
//echo $url;die;
$content = $spider->getChapterContent($url);
//echo $content;die;
$content = $spider->filterChapterContent($content);
print_r($content);die;
var_dump($spider->isCanSave($content));die;
var_dump($spider->isImage($content));die;
/**/
$list = \collect\site\Xiaoshuomm::getNewList();


function filterChapterName($chapterName)
{
    $patterns = array(
        '(\(|（)[^\)]{4,}(\)|）)',
        '【第.{0,2}更】',
        '第.{0,2}更',
        '啃文书库',
        '【.{0,4}】',
        '(\(|（|【)求(.*?)(\)|）|】)',

    );
    /**/
    foreach ($patterns as $pattern)
    {
        $chapterName = trim(preg_replace('/'.$pattern.'/i', '', $chapterName));
    }
    /**/
    $find = array('正文', 'VIP卷', 'VIP章节目录', '章节目录', '网友上传章节', '网友上传', '.', ',', '，', '。', '?', '？', ':', '：', '、', '!', '！', '(', ')', '）', '（', '~', '～', '…', '—','－','-','．');
    $chapterName = trim(str_replace($find, '', $chapterName));
    $chapterName = str_replace(array('１', '２', '３', '４', '５', '６', '７', '８', '９', '０', '／'), array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, '/'), $chapterName);
    //$chapterName = rtrim($chapterName, '图,t,T'); // 去掉后面那个图字（事实证明这个函数不安全，中文的时候有时最后会多个问号，下面用其它办法解决）
    if (preg_match('/(图|t|T)$/i', $chapterName))
    {
        // 解决汉字不给力的问题
        $array = cutUTF8String($chapterName);
        unset($array[count($array) - 1]);
        $chapterName = trim(implode('', $array));
    }

    return $chapterName;
}

function getstr($string, $length, $encoding  = 'utf-8')
{
    $string = trim($string);

    if($length && strlen($string) > $length) {
        //截断字符
        $wordscut = '';
        if(strtolower($encoding) == 'utf-8') {
            //utf8编码
            $n = 0;
            $tn = 0;
            $noc = 0;
            while ($n < strlen($string)) {
                $t = ord($string[$n]);
                if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
                    $tn = 1;
                    $n++;
                    $noc++;
                } elseif(194 <= $t && $t <= 223) {
                    $tn = 2;
                    $n += 2;
                    $noc += 2;
                } elseif(224 <= $t && $t < 239) {
                    $tn = 3;
                    $n += 3;
                    $noc += 2;
                } elseif(240 <= $t && $t <= 247) {
                    $tn = 4;
                    $n += 4;
                    $noc += 2;
                } elseif(248 <= $t && $t <= 251) {
                    $tn = 5;
                    $n += 5;
                    $noc += 2;
                } elseif($t == 252 || $t == 253) {
                    $tn = 6;
                    $n += 6;
                    $noc += 2;
                } else {
                    $n++;
                }
                if ($noc >= $length) {
                    break;
                }
            }
            if ($noc > $length) {
                $n -= $tn;
            }
            $wordscut = substr($string, 0, $n);
        } else {
            for($i = 0; $i < $length - 1; $i++) {
                if(ord($string[$i]) > 127) {
                    $wordscut .= $string[$i].$string[$i + 1];
                    $i++;
                } else {
                    $wordscut .= $string[$i];
                }
            }
        }
        $string = $wordscut;
    }
    return trim($string);
}