Typecho 文章批量导出为 md 文件

Typecho 文章批量导出为 md 文件

William
2024-02-18 / 0 评论 / 7 阅读 / 正在检测是否收录...

1、复制下方代码在 typecho 网站根目录创建 converter.php 文件

<?php
// 运行 php converter.php
$db = new mysqli();
// 根据实际情况更改
$db->connect('localhost','数据库用户名','数据库密码','数据库名称');
$prefix = 'typecho_';
$sql = <<<TEXT
select title,text,created,category,tags from {$prefix}contents c,
 (select cid,group_concat(m.name) tags from {$prefix}metas m,{$prefix}relationships r where m.mid=r.mid and m.type='tag' group by cid ) t1,
(select cid,m.name category from {$prefix}metas m,{$prefix}relationships r where m.mid=r.mid and m.type='category') t2
where t1.cid=t2.cid and c.cid=t1.cid
TEXT;
$res = $db->query($sql);
if ($res) {
    if ($res->num_rows > 0) {
        while ($r = $res->fetch_object()) {
            $_c = @date('Y-m-d H:i:s', $r->created);
            $_t = str_replace('<!--markdown-->', '', $r->text);
            $_tmp = <<<TMP
{$_t}
TMP;
            // windows下把文件名从UTF-8编码转换为GBK编码,避免出现生成的文件名为乱码的情况
            if (strpos(PHP_OS, "WIN") !== false) {
                $name = iconv("UTF-8", "GBK//IGNORE", $r->title);
                echo $name.'<br>';
            } else {
                $name = $r->title;
                echo $name.'<br>';
            }
            // 替换不合法文件名字符
            file_put_contents(str_replace(array(" ", "?", "\\", "/", ":", "|", "*"), '-', $name) . ".md", $_tmp);
        }
    }
    $res->free();
}
$db->close();

2、使用文本编辑器打开 converter.php 将上方代码中 $db->connect('localhost','数据库用户名','数据库密码','数据库用户名'); 的信息更改为你自己的数据库信息

3、打开你的服务器终端

4、输入 php converter.php ,就会自动转换为md文件啦

0

评论 (0)

取消