How to generate a CHM document in PHP

Source: Internet
Author: User

A class and a hhc.exe and a hha.dll.

Used to generate a CHM project file containing an HTML directory, and then generate a. CHM manual by HHP the project file and the content file.

<?php/* Function listdirtree ($dirName = null) * * Feature lists all files and subdirectories under directory * * * Parameters $dirName Directory Name * * Return directory structure array false to Failed/function Listdir ($dirName = null) {if (empty ($dirName)) exit ("Ibfilesystem:directory I
        s 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)) {//is a directory, recursive $tree 2 =listdir ($filePath); $tree = $tree 2?
                        Array_merge ($tree, $tree 2): $tree;
                        else {//is a file, added to the current array $tree [] = $filePath;
            }} closedir ($DH); else {exit ("IbfilesYstem:can not open Directory $dirName. ");
            Returns the current $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:d
        Irectory is empty. ");
                if (Is_dir ($dirName)) {if ($dh = Opendir ($dirName)) {$tree = array (); while ($file = Readdir ($DH))!== false) {if ($file!= "." && $file!= "..." && stri POS ($remove, $file) = = False) {$filePath = $dirName. Directory_separator.
                        $file;
                            if (Is_dir ($filePath)) {//is a directory, recursive $arr = Listdirtree ($filePath, $remove);
                            Natsort ($arr);
                  $tree [$file] = $arr;      else {//is a file, added to the current array $tree [] = $filePath;
            }} closedir ($DH);
            else {exit ("Ibfilesystem:can not open Directory $dirName.");
        //Returns the current $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 ';
            The Public Function build () {$this->buildhhp ();
            $this-&GT;BUILDHHC ();
        $this->buildhhk ();
            The 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-&GT;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);
            The 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 (DI
                    Rectory_separator, $value);
                    $title = Array_pop ($title);
                    $title = RTrim ($title, '. html '); $list [] = <<<eof <li><object type= "Text/sitemap" > <param name= "name" value= "{$title}" & Gt
        <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}" &G
        T
<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 "> & Lt;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);
            The 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}" &
        Gt
                    <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> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.