想要使用str_slug将文本更改为Slug.它适用于不同情况,但我希望它在不更改UpperCases的情况下工作,即
例如:您好—世界=>你好,世界
有没有办法得到我想要的?
解决方法:
如关于laracasts.com的问题所述,您可以创建自己的helper函数版本,从而省去了mb_strtolower():
public static function slug($title, $separator = '-', $language = 'en')
{
$title = static::ascii($title, $language);
// Convert all dashes/underscores into separator
$flip = $separator == '-' ? '_' : '-';
$title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);
// Replace @ with the word 'at'
$title = str_replace('@', $separator.'at'.$separator, $title);
// Remove all characters that are not the separator, letters, numbers, or whitespace.
// With lower case: $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', mb_strtolower($title));
$title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', $title);
// Replace all separator characters and whitespace by a single separator
$title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
return trim($title, $separator);
}
标签:laravel,string,php
来源: https://codeday.me/bug/20191025/1928369.html