简体   繁体   English

如何使用 PHP 在 Laravel 7 中将 PascalCase 字符串转换为可用的 slug?

[英]How can I convert PascalCase string to a usable slug in Laravel 7 with PHP?

I currently have a variable with the value of "ExtraOption" .我目前有一个值为"ExtraOption"的变量。 I wonder if it is possible (with Laravel helpers perhaps) convert this string into a usable slug.我想知道是否有可能(也许使用 Laravel 助手)将此字符串转换为可用的 slug。 For example: "extra-option" .例如: "extra-option"

Can anyone help me out with a single, clean solution?任何人都可以帮我解决一个单一的、干净的解决方案吗?

You can use Str::kebab() method to converts the given string to kebab-case:您可以使用Str::kebab()方法将给定的字符串转换为 kebab-case:

use Illuminate\Support\Str;

$converted = Str::kebab('ExtraOption');

// extra-option

Laravel 7 has new feature Fluent String Operations which provides a variety of helpful string manipulation functions. Laravel 7 新特性Fluent String Operations提供了各种有用的字符串操作功能。

kebab

The kebab method converts the given string to kebab-case: kebab 方法将给定的字符串转换为 kebab-case:

use Illuminate\Support\Str;

$converted = Str::of('ExtraOption')->kebab();

// extra-option

Try using this code:尝试使用此代码:

use Illuminate\Support\Str;

...
Str::slug(implode(' ', preg_split('/(?=[A-Z])/', 'camelCaseToSlug')))
> camel-case-to-slug
...

Hope this helps you希望这对你有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM