index.php代码,index.php

error_reporting(E_ALL);

set_time_limit(0);

ini_set('display_errors', 'on');

date_default_timezone_set('Asia/Shanghai');

define('CHM_PATH', __DIR__ . '/php_enhanced_zh/');

/**

* 搜索html文件

* @return array

*/

function searchHtml()

{

$pattern = CHM_PATH . 'res/*.html';

$files = glob($pattern);

return $files;

}

/**

* 获取文件内容并转换为UTF-8编码

* @param string $file

* @return string

*/

function getFileWithUTF8($file)

{

$content = file_get_contents($file);

$content = iconv('gbk', 'utf-8', $content);

return $content;

}

/**

* 修复导航条

* @param string $content

* @return string

*/

function fixNavbar($content)

{

$pattern = [

'#

\? (.*?)#',

'#

(.*?) \?#'

];

$replacement = [

'

« \2',

'

\2 »'

];

$content = preg_replace($pattern, $replacement, $content);

return $content;

}

/**

* 修复面包屑

* @param string $content

* @return string

*/

function fixBreadcrumbs($content)

{

$search = '

$replace = '

$content = str_replace($search, $replace, $content);

return $content;

}

/**

* 解析页面标题

* @param type $content

* @return string

*/

function parseTitle($content)

{

$title = [];

$pattern = '#

(.+?)
#s';

preg_match($pattern, $content, $matches);

if (!$matches) {

$pattern = '#

]?)#s';

preg_match($pattern, $content, $matches);

}

$subject = isset($matches[0]) ? $matches[0] : '';

$patterns = [

'#

]*)>(.+?)

#s',

'#

]*)>(.+?)

#s',

'#

]*)>(.+?)

#s'

];

foreach ($patterns as $pattern) {

preg_match_all($pattern, $subject, $matches);

if ($matches[0]) {

foreach ($matches[2] as $v) {

$v = strip_tags($v);

$v = trim($v);

$v = preg_replace('#\s+#', ' ', $v);

$title[] = $v;

}

break;

}

}

$pattern = '#

(.+?)#s';

preg_match($pattern, $content, $matches);

if ($matches) {

$v = preg_replace('#\s+#', ' ', $matches[1]);

$title[] = $v;

}

$title = array_unique($title);

return $title;

}

/**

* 以gbk编码保存文件内容

* @param string $file

* @param string $content

*/

function saveFileWithGBK($file, $content)

{

$content = iconv('utf-8', 'gbk', $content);

file_put_contents($file, $content);

}

/**

* 重建目录文件

* @param array $titles

*/

function buildHhcFile(&$titles)

{

$hhcFile = CHM_PATH . 'php_manual_zh.hhc';

$content = getFileWithUTF8($hhcFile);

$pattern = '#(?:\s*)(?:\s*)(?:\s*)#s';

preg_match_all($pattern, $content, $matches);

foreach ($matches[0] as $key => $value) {

$oldTitle = $matches[1][$key];

$shortPath = $matches[2][$key];

$nextTag = $matches[3][$key];

if ($oldTitle) {

$title = preg_replace('#\s+#', ' ', $oldTitle);

} else {

$title = isset($titles[$shortPath]) ? $titles[$shortPath] : [];

$title = isset($title[0]) ? $title[0] : '无标题';

}

$replace = str_replace('value="' . $oldTitle . '"', 'value="' . $title . '"', $value);

if ($nextTag != 'ul') {

//没有子节点的话用这个图标(默认是带问号的图标)

$replace = str_replace('', '', $replace);

}

$content = str_replace($value, $replace, $content);

}

saveFileWithGBK($hhcFile, $content);

}

/**

* 重建索引文件

* @param array $titles

*/

function buildHhkFile(&$titles)

{

$hhkFile = CHM_PATH . 'php_manual_zh.hhk';

$content = getFileWithUTF8($hhkFile);

$content = preg_replace('#

  • (.+)
#s', '
  • PLACEHOLDER
', $content);

$html = '';

foreach ($titles as $shortPath => $title) {

foreach ($title as $value) {

$html .= '

';

}

}

$content = str_replace('PLACEHOLDER', $html, $content);

saveFileWithGBK($hhkFile, $content);

}

/**

* 生成项目配置文件

* @param array $htmlFiles

*/

function buildHhpFile(&$htmlFiles)

{

$date = date('Ymd');

$content = <<

[OPTIONS]

Compatibility=1.1 or later

Compiled file=php_enhanced_zh.chm

Contents file=php_manual_zh.hhc

Default Window=phpchm

Default topic=res\index.html

Display compile progress=Yes

Full-text search=Yes

Index file=php_manual_zh.hhk

Language=0x804 中文(简体,中国)

Title=PHP中文手册

[WINDOWS]

phpchm="PHP中文手册 - {$date}","php_manual_zh.hhc","php_manual_zh.hhk","res\index.html","res\index.html",,,,,0x23520,,0x387e,,0x1000000,,,,,,0

[FILES]

EOT;

foreach ($htmlFiles as $filePath) {

$shortPath = str_replace([CHM_PATH, '/'], ['', '\\'], $filePath);

$content .= "\n" . $shortPath;

}

$hhpFile = CHM_PATH . 'php_manual_zh.hhp';

saveFileWithGBK($hhpFile, $content);

}

$startTime = time();

$titles = [];

echo "正在检索 html 文件...\n";

$htmlFiles = searchHtml();

if (!$htmlFiles) {

echo "未检索到 html 文件,请下载手册原文件并解压至 " . CHM_PATH . " 目录\n";

echo "下载地址 https://www.php.net/distributions/manual/php_enhanced_zh.chm\n";

exit;

}

echo "开始处理 html 文件\n";

$count = count($htmlFiles);

$progress = 0;

foreach ($htmlFiles as $key => $filePath) {

$content = getFileWithUTF8($filePath);

$content = fixNavbar($content);

$content = fixBreadcrumbs($content);

$title = parseTitle($content);

$shortPath = str_replace([CHM_PATH, '\\'], ['', '/'], $filePath);

$titles[$shortPath] = $title;

saveFileWithGBK($filePath, $content);

$newProgress = floor(($key + 1) / $count * 100);

if ($progress != $newProgress) {

$progress = $newProgress;

echo "\r当前进度 {$progress}%";

if ($progress == 100) {

echo "\n";

}

}

}

echo "正在生成目录文件 php_manual_zh.hhc\n";

buildHhcFile($titles);

echo "正在生成索引文件 php_manual_zh.hhk\n";

buildHhkFile($titles);

echo "正在生成项目文件 php_manual_zh.hhp\n";

buildHhpFile($htmlFiles);

$useTime = time() - $startTime;

$minute = floor($useTime / 60);

$second = $useTime % 60;

echo "已完成,用时 {$minute} 分 {$second} 秒\n";

一键复制

编辑

Web IDE

原始数据

按行查看

历史

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值