php - 用德语变音符号生成 slug

标签 php regex

我尝试从字符串生成一个 slug,但我在使用德语变音符号时遇到了一些问题:

$text = 'Ein schöner Text';
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
$text = trim($text, '-');
$text = iconv('utf-8', 'ASCII//TRANSLIT', $text);
$text = strtolower($text);
$text = preg_replace('~[^-\w]+~', '', $text);

结果应该是:'ein-schoener-text'

最佳答案

将第二个 preg_replace 行更改为以下内容,因为要匹配任何语言的任何字母,您需要使用 \p{L} 模式。

$text = preg_replace('~[^\p{L}\d]+~u', '-', $text);

代码:

<?php
$text = 'Ein schöner Text';
$text = preg_replace('~[^\p{L}\d]+~u', '-', $text);
$text = trim($text, '-');
$text = iconv('utf-8', 'ASCII//TRANSLIT', $text);
$text = strtolower($text);
$text = preg_replace('~[^-\w]+~', '', $text);
echo $text;
?>

输出:

ein-schoner-text

关于php - 用德语变音符号生成 slug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26054846/

相关文章:

regex - 正则表达式查询es 1.7

php - 如何从php中的链接下载视频?

PHP : Decrypt password from hash

php - 无法在 mysql 数据库中更新密码以及如何在注册页面中插入确认密码

php - 显示来自 MySQL 的表和链接以从数据库中删除每一行

php - 如何在 PHP 中用 '*' 替换不在 [0x5E10, 0x7F35] 范围内的字符?

php - 正则表达式不能正确处理土耳其字符

javascript - 正则表达式匹配基本媒体查询

php - 将MySQL数据合并到同一个列表中

javascript - 匹配除少数字符外的 ascii 字符