MySQL查询优化:将Em Dash和En Dash替换为单破折号生成Slug
解决MySQL生成Slug时特殊破折号(Em Dash/En Dash)的问题
原查询在处理包含En Dash(–)或Em Dash(—)的字符串时,无法正确转换这些特殊符号,导致生成的slug出现多余连字符(例如arrival-–-free-time-or-optional-activities)。以下是优化后的查询,可解决该问题:
SELECT *, LOWER( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( TRIM(name), ':', ''), ')', ''), '(', ''), ',', ''), '\\', ''), '\/', ''), '\\"', ''), '?', ''), '\'', ''), '&', ''), '!', ''), '.', ''), ' ', '-'), '–', '-'), -- 替换En Dash为普通连字符 '—', '-'), -- 替换Em Dash为普通连字符 '--', '-'), '--', '-' ) AS slug FROM program;
关键修改说明
- 新增特殊破折号替换:在替换空格为连字符后,添加对En Dash(
'–')和Em Dash('—')的替换,将它们统一转为普通连字符'-' - 重复连字符清理:保留原有的多次
REPLACE('--', '-')操作,确保所有连续的连字符被合并为单个连字符
优化后生成的slug会符合预期:arrival-free-time-or-optional-activities
内容的提问来源于stack exchange,提问作者Ered
相关产品推荐
相关产品推荐

