前言
在我使用halo写文章时,为文章命名后,同时要设置一个文章别名用来访问,而默认生成的是以标题拼音命名,这样地址栏中会不美观,所以需要为文章生成一个唯一的ID。
实现方法
这里我参考了hexo-abbrlink这个项目,首先对文章标题进行一次CRC16/CRC32加密处理,生成一个ID,同时可选择是否以十六进制的形式显示。

这里我用PHP进行编写,其中PHP自带了crc32()的函数,而crc16()需要自己实现,可以参考我的另一篇文章: 使用PHP实现CRC16算法
CRC16:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 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); }
|
实现页面
为了方便使用,我打算在网页中放置一个表单,允许输入文字标题,选择加密算法,选择是否显示为十六进制
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <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>
|
当提交后,会携带参数title、type(crc16 / crc32)、hex(on),下一步中将获取这三个参数。
实现功能
请求后显示值
当发送请求后,可以将传入的值显示到表单中(请求后默认会消失),下面代码只展示需要添加的部分
1 2 3 4 5 6 7 8 9 10
| <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" : ""; ?>>
|
处理并显示结果
请求后,需要将处理结果显示到合适的位置,我将结果放置在提交按钮的下方
1 2 3 4 5 6 7 8 9 10
| <?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 } ?>
|
执行效果
这样就实现了需要的效果了

完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <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); } ?>
|
结尾
这篇文章到这里就结束了,感谢耐心阅读,如果遇到问题可以在下方评论区中讨论。