[英]Generate slug with german umlauts
我尝试从字符串生成子段,但是我遇到了德国变音符的一些问题:
$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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.