我尝试从字符串中生成一个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'
答案 0 :(得分:0)
将第二个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