Код: Выделить всё
public function camelToKebab($string, $us = "-")
{
// insert hyphen between any letter and the beginning of a numeric chain
$string = preg_replace('/([a-z]+)([0-9]+)/i', '$1'.$us.'$2', $string);
// insert hyphen between any lower-to-upper-case letter chain
$string = preg_replace('/([a-z]+)([A-Z]+)/', '$1'.$us.'$2', $string);
// insert hyphen between the end of a numeric chain and the beginning of an alpha chain
$string = preg_replace('/([0-9]+)([a-z]+)/i', '$1' . $us . '$2', $string);
// Lowercase
$string = strtolower($string);
return $string;
}
Код: Выделить всё
array('input' => 'output')Код: Выделить всё
$test_values = [
'foo' => 'foo',
'fooBar' => 'foo-bar',
'foo123' => 'foo-123',
'123Foo' => '123-foo',
'fooBar123' => 'foo-bar-123',
'foo123Bar' => 'foo-123-bar',
'123FooBar' => '123-foo-bar',
];
ПРИМЕЧАНИЕ. Ссылаясь на этот пост, мои исследования показали мне регулярное выражение preg_replace(), которое дает мне почти желаемый результат. , за исключением того, что в примере с foo123 не работает преобразование его в foo-123.
Подробнее здесь: https://stackoverflow.com/questions/405 ... -kebab-cas
Мобильная версия