侧边栏壁纸
博主头像
小斯小站 博主等级

用心分享技术

  • 累计撰写 37 篇文章
  • 累计创建 75 个标签
  • 累计收到 9 条评论

目 录CONTENT

文章目录

为文章生成一个唯一的ID

SCH小斯
2024-04-25 / 1 评论 / 0 点赞 / 42 阅读 / 7184 字
温馨提示:
本文最后更新于 2024-05-04,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

前言

在我使用halo写文章时,为文章命名后,同时要设置一个文章别名用来访问,而默认生成的是以标题拼音命名,这样地址栏中会不美观,所以需要为文章生成一个唯一的ID。

设置文章标题

实现方法

这里我参考了hexo- abbrlink这个项目,首先对文章标题进行一次CRC16/CRC32加密处理,生成一个ID,同时可选择是否以十六进制的形式显示。

这里我用PHP进行编写,其中PHP自带了crc32()的函数,而crc16()需要自己实现,可以参考我的另一篇文章:使用PHP实现CRC16算法

CRC16:

function crc16($str): int
{
    $crc = 0xffff;
    for ($i = 0; $i < strlen($str); $i++) {
        $crc = ($crc >> 8) ^ ord($str[$i]);
        for ($j = 0; $j < 8; ++$j) {
            if (($crc & 0x0001) == 0) {
                $crc >>= 1;
            } else {
                $crc >>= 1;
                $crc ^= 0xa001;
            }
        }
    }
    return (($crc & 0xff) << 8) | (($crc >> 8) & 0xff);
}

实现页面

为了方便使用,我打算在网页中放置一个表单,允许输入文字标题,选择加密算法,选择是否显示为十六进制

<form>
    <h4>文章ID生成</h4>
    <label for="title">文章标题:</label>
    <input type="text" id="title" name="title" placeholder="输入文章标题" required>
    <p>加密算法:</p>
    <input type="radio" name="type" id="type1" value="crc16">
    <label for="type1">CRC16</label>
    <input type="radio" name="type" id="type2" value="crc32">>
    <label for="type2">CRC32</label><br>
    <input type="checkbox" id="hex" name="hex">
    <label for="hex">十六进制输出</label><br>
    <input type="submit" value="生成">
    <p>文章ID:xxxx</p>
</form>

当提交后,会携带参数titletype(crc16 / crc32)hex(on),下一步中将获取这三个参数。

实现功能

请求后显示值

当发送请求后,可以将传入的值显示到表单中(请求后默认会消失),下面代码只展示需要添加的部分

<!-- ··· -->
<input type="text" id="title" name="title" placeholder="输入文章标题" required
           value="<?php echo $_GET["title"]; ?>">
<!-- ··· -->
<input type="radio" name="type" id="type1" value="crc16" <?php echo @$_GET["type"] == "crc16" || @$_GET["type"] != "crc32"? "checked" : ""; ?>>
<!-- ··· -->
<input type="radio" name="type" id="type2" value="crc32" <?php echo @$_GET["type"] == "crc32" ? "checked" : ""; ?>>
<!--- ··· -->
<input type="checkbox" id="hex" name="hex" <?php echo @$_GET["hex"] == "on" ? "checked" : ""; ?>>
<!-- ··· -->

处理并显示结果

请求后,需要将处理结果显示到合适的位置,我将结果放置在提交按钮的下方

<?php if (isset($_GET["title"])) { ?>
    <?php
        $title = $_GET["title"];
        $type = $_GET["type"];
        $hex = $_GET["hex"];
        $crc = $type == "crc16" ? crc16($title) : crc32($title);
        if ($hex == "on") $crc = strtoupper(dechex($crc));
    ?>
    <p>文章ID:<?php echo $crc; ?></p>
<?php } ?>

执行效果

执行效果

这样就实现了需要的效果了

完整代码

<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>文章ID生成</title>
</head>
<body>
<form>
    <h4>文章ID生成</h4>
    <label for="title">文章标题:</label>
    <input type="text" id="title" name="title" placeholder="输入文章标题" required value="<?php echo $_GET["title"]; ?>">
    <p>加密算法:</p>
    <input type="radio" name="type" id="type1" value="crc16" <?php echo @$_GET["type"] == "crc16" || @$_GET["type"] != "crc32" ? "checked" : ""; ?>>
    <label for="type1">CRC16</label>
    <input type="radio" name="type" id="type2" value="crc32" <?php echo @$_GET["type"] == "crc32" ? "checked" : ""; ?>>
    <label for="type2">CRC32</label><br>
    <input type="checkbox" id="hex" name="hex" <?php echo @$_GET["hex"] == "on" ? "checked" : ""; ?>>
    <label for="hex">十六进制输出</label><br>
    <input type="submit" value="生成">
    <?php if (isset($_GET["title"])) { ?>
        <?php
        $title = $_GET["title"];
        $type = $_GET["type"];
        $hex = $_GET["hex"];
        $crc = $type == "crc16" ? crc16($title) : crc32($title);
        if ($hex == "on") $crc = strtoupper(dechex($crc));
        ?>
        <p>文章ID:<?php echo $crc; ?></p>
    <?php } ?>
</form>
</body>
</html>
<?php
function crc16($str): int
{
    $crc = 0xffff;
    for ($i = 0; $i < strlen($str); $i++) {
        $crc = ($crc >> 8) ^ ord($str[$i]);
        for ($j = 0; $j < 8; ++$j) {
            if (($crc & 0x0001) == 0) {
                $crc >>= 1;
            } else {
                $crc >>= 1;
                $crc ^= 0xa001;
            }
        }
    }
    return (($crc & 0xff) << 8) | (($crc >> 8) & 0xff);
}
?>

结尾

这篇文章到这里就结束了,感谢耐心阅读,如果遇到问题可以在下方评论区中讨论。

0

评论区