如何为数据库字符串值生成 SEF 别名
我有一个名为 games 的数据库表,为了简化起见,它包含列“id”和“gametitle”,并且已经填充了大约 7,000 行。
我想要做的是有一段 SQL 可以为我可以在 URL 中使用的游戏标题生成 SEF 别名(我在网站上成功运行了 URL 重写)。
新字段将输入到名为“gamealias”的新列中。
例如,如果我有一个游戏标题“光环:战斗进化”,它会删除所有特殊字符并产生类似于“光环战斗进化”的内容。表中的所有别名都必须是唯一的,因为它们将在游戏 URL 中使用...例如 http://thegamesdb.net/game/halo-combat-evolved/
此外,如果在插入新行时,可以从 SQL 自动生成别名,而无需处理,那就太好了它在 PHP 中(也许使用sql 字段默认值以某种方式),尽管如果在 SQL 中不可能神奇地实现这一点,我不介意必须在 PHP 中执行此操作。
I have a DB table named games which lets just say for simplification contains the columns "id" and "gametitle" and has roughly 7,000 rows already populated.
What I would like to do is have a piece of SQL that would generate a SEF alias for the gametitle that I can use in the URL (I have URL Rewriting successfully running on the site).
The new field would be entered into a new column named "gamealias".
So for instance, if I had a gametitle of "Halo: Combat Evolved" it would drop any special characters and produce something similar to "halo-combat-evolved". All the aliases in the table would have to be unique as they are going to be used in the games url... e.g. http://thegamesdb.net/game/halo-combat-evolved/
Also, it would be nice if upon insertion of a new row, the alias could be generated automatically from SQL without having to handle it in PHP (maybe using the sql fields default value somehow), although I don't mind having to do this in PHP if it's not possible magically in SQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这可以在 MySQL 中使用触发器和正则表达式替换 UDF 来完成,但仅使用 PHP 可能是一个更简单的路线。如果您将标题的主键与字符串集成在一起,您可以轻松保证游戏别名的唯一性。例如,考虑会输出“halo-combat-evolved-321”的东西。
I think this could be done in MySQL using a trigger and a regex-replace UDF, but it would probably be a simpler route just using PHP. You could easily guarantee gamealias uniqueness if you integrated the title's primary key in with the string. For example, consider something that would output "halo-combat-evolved-321".