程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> 如何在PHP中生成chm文檔

如何在PHP中生成chm文檔

編輯:PHP綜合

一個類和hhc.exe還有hha.dll

用於生成包含html目錄的chm項目文件 ,然後通過hhp項目文件和內容文件生成 .chm手冊。

<?php
    /* 函數 listDirTree( $dirName = null )
    ** 功能 列出目錄下所有文件及子目錄
    ** 參數 $dirName 目錄名稱
    ** 返回 目錄結構數組 false為失敗
    */
    function listDir($dirName = null) {
        if (empty($dirName))
            exit("IBFileSystem: directory is empty.");
        if (is_dir($dirName)) {
            if ($dh = opendir($dirName)) {
                $tree = array();
                while (( $file = readdir($dh) ) !== false) {
                    if ($file != "." && $file != "..") {
                        $filePath = $dirName . DIRECTORY_SEPARATOR . $file;
                        if (is_dir($filePath)) { //為目錄,遞歸
                            $tree2 =listDir($filePath);
                            $tree = $tree2? array_merge($tree,$tree2):$tree;
                        } else { //為文件,添加到當前數組
                            $tree[] = $filePath;
                        }
                    }
                }
                closedir($dh);
            } else {
                exit("IBFileSystem: can not open directory $dirName.");
            }
            //返回當前的$tree
            $tree = array_unique($tree);
            natsort($tree);
            return $tree;
        } else {
            exit("IBFileSystem: $dirName is not a directory.");
        }
    }
    function listDirTree($dirName = null,$remove) {
        if (empty($dirName))
            exit("IBFileSystem: directory is empty.");
        if (is_dir($dirName)) {
            if ($dh = opendir($dirName)) {
                $tree = array();
                while (( $file = readdir($dh) ) !== false) {
                    if ($file != "." && $file != ".." && stripos($remove, $file) === false) {
                        $filePath = $dirName . DIRECTORY_SEPARATOR . $file;
                        if (is_dir($filePath)) { //為目錄,遞歸
                            $arr = listDirTree($filePath,$remove);
                            natsort($arr);
                            $tree[$file] = $arr;
                        } else { //為文件,添加到當前數組
                            $tree[] = $filePath;
                        }
                    }
                }
                closedir($dh);
            } else {
                exit("IBFileSystem: can not open directory $dirName.");
            }
                          
            //返回當前的$tree
            return $tree;
        } else {
            exit("IBFileSystem: $dirName is not a directory.");
        }
    }
    function cmp($a,$b){
        $a = (int)$a;
        $b = (int)$b;
        if($a == $b)    return 0;
        return ($a>$b)? 1:-1;
    }
    class chmBuilder{
        // const version = 0.1;
        public $chm_name;
        public $chm_path;
        public $chm_hhp;
        public $chm_hhc;
        public $chm_hhk;
        public $chm_uninclude_dirs;
        public $chm_uninclude_files;
        public $chm_image_type;
        public $chm_first_open;
        public $chm_title;
        public function __construct($chm_name='your_chm',$chm_path='',$chm_uninclude_dirs,$chm_uninclude_files){
            $this->chm_name = $chm_name;
            $this->chm_path = $chm_path;
            $this->chm_uninclude_dirs = $chm_uninclude_dirs;
            $this->chm_uninclude_files = $chm_uninclude_files;
            $this->chm_image_type = 'Folder';
        }
        public function build(){
            $this->buildHhp();
            $this->buildHhc();
            $this->buildHhk();
        }
        public function buildHhp(){
            $manual_files = listDir($this->chm_path);
            $files = implode(PHP_EOL, $manual_files);
            $this->chm_first_open = iconv('UTF-8', 'GB2312', $this->chm_first_open);
            $this->chm_title = iconv('UTF-8', 'GB2312', $this->chm_title);
            $tpl = <<<eof
[OPTIONS]
Compatibility=1.1 or later
Compiled file={$this->chm_path}/{$this->chm_name}.chm
Contents file={$this->chm_hhc}.hhc
COPYRIGHT=www.thinkphp.cn
Display compile progress=No
Default topic={$this->chm_first_open}
Error log file=chm_builder.Log
Full-text search=Yes
Index file={$this->chm_hhk}.hhk
ImageType={$this->chm_image_type}
Language=0x804
Title={$this->chm_title}
[FILES]
{$files}
eof;
            file_put_contents("{$this->chm_path}/{$this->chm_hhp}.hhp", $tpl);
        }
        public function buildHhc(){
            $list = array();
            $file_tree = listDirTree($this->chm_path,"{$this->chm_hhp} {$this->chm_uninclude_dirs}{$this->chm_uninclude_files}");
            uksort($file_tree, 'cmp');
            foreach ($file_tree as $key => $value) {
                if(is_string($value)){
                    $title = explode(DIRECTORY_SEPARATOR, $value);
                    $title = array_pop($title);
                    $title = rtrim($title,'.html');
                    $list[] = <<<eof
    <LI><OBJECT type="text/sitemap">
        <param name="Name" value="{$title}">
        <param name="Local" value="{$value}">
        </OBJECT>
eof;
                }else{
                    $child = array();
                    foreach ($value as $k => $val) {
                        $title = explode(DIRECTORY_SEPARATOR, $val);
                        $title = array_pop($title);
                        $title = rtrim($title,'.html');
                        $child[] = <<<eof
        <LI><OBJECT type="text/sitemap">
            <param name="Name" value="{$title}">
            <param name="Local" value="{$val}">
            <param name="ImageNumber" value="9">
            </OBJECT>
eof;
                    }
                    $child = implode(PHP_EOL, $child);
                    $list[] = <<<eof
    <LI> <OBJECT type="text/sitemap">
        <param name="Name" value="{$key}">
        <param name="ImageNumber" value="1">
        </OBJECT>
    <UL>  
{$child}
    </UL>  
eof;
                }
            }
            $list = implode(PHP_EOL, $list);
            $tpl = <<<eof
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<HTML>
<HEAD>
<meta name="GENERATOR" content="yangweijie code-tech.diandian.com">
<!-- Sitemap 1.0 -->
</HEAD><BODY>
<OBJECT type="text/site properties">
    <param name="ExWindow Styles" value="0x200">
    <param name="Window Styles" value="0x800025">
    <param name="Font" value="MS Sans Serif,10,0">
</OBJECT>
<UL>
{$list}
</UL>
</BODY></HTML>
eof;
            file_put_contents("{$this->chm_path}/{$this->chm_hhc}.hhc", $tpl);
        }
        public function buildHhk(){
            $list = array();
            $file_tree = listDir($this->chm_path);
            foreach ($file_tree as $key => $value) {
                if(is_string($value)){
                    if(stripos($value, '.html')){
                        $title = explode(DIRECTORY_SEPARATOR, $value);
                        $title = array_pop($title);
                        $title = rtrim($title,'.html');
                        $list[] = <<<eof
    <LI><OBJECT type="text/sitemap">
        <param name="Name" value="{$title}">
        <param name="Local" value="{$value}">
        </OBJECT>
eof;
                    }
                }
            }
            $list = implode(PHP_EOL, $list);
            $tpl = <<<eof
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<HTML>
<HEAD>
<meta name="GENERATOR" content="yangweijie code-tech.diandian.com">
<!-- Sitemap 1.0 -->
</HEAD><BODY>
<UL>
{$list}
</UL>
</BODY></HTML>
eof;
            file_put_contents("{$this->chm_path}/{$this->chm_hhk}.hhk", $tpl);
        }

        public function makeChm(){
            if(!is_file("{$this->chm_path}/{$this->chm_hhp}.hhp"))  
                return "build error:can't generate *.hhp file!";
            $command = "hhc {$this->chm_path}/{$this->chm_hhp}.hhp";
            system($command);
            if(file_exists("{$this->chm_path}/{$this->chm_name}.chm"))
                return true;
            else
                return 'generate chm failed!';
        }
    }
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved