|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jasny; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Turn a sentence, camelCase, snake_case or kabab-case into camelCase |
|
7
|
|
|
* |
|
8
|
|
|
* @param string $string |
|
9
|
|
|
* @return string |
|
10
|
|
|
*/ |
|
11
|
|
|
function camelcase($string) |
|
12
|
|
|
{ |
|
13
|
1 |
|
$sentence = preg_replace('/[\W_]+/', ' ', $string); |
|
14
|
1 |
|
return lcfirst(str_replace(' ', '', ucwords($sentence))); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Turn a sentence, camelCase, snake_case or kabab-case into StudlyCase |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $string |
|
21
|
|
|
* @return string |
|
22
|
|
|
*/ |
|
23
|
|
|
function studlycase($string) |
|
24
|
|
|
{ |
|
25
|
1 |
|
$sentence = preg_replace('/[\W_]+/', ' ', $string); |
|
26
|
1 |
|
return str_replace(' ', '', ucwords($sentence)); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Turn a sentence, camelCase, StudlyCase or kabab-case into snake_case |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $string |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
function snakecase($string) |
|
36
|
|
|
{ |
|
37
|
1 |
|
$sentence = strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1 $2', $string)); |
|
38
|
1 |
|
return preg_replace('/[\W\_]+/', '_', $sentence); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Turn a sentence, camelCase, StudlyCase or snake_case into kabab-case |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $string |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
function kababcase($string) |
|
48
|
|
|
{ |
|
49
|
1 |
|
$sentence = strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1 $2', $string)); |
|
50
|
1 |
|
return preg_replace('/[\W\_]+/', '-', $sentence); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Turn StudlyCase, camelCase, snake_case or kabab-case into a sentence. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $string |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
function uncase($string) |
|
60
|
|
|
{ |
|
61
|
1 |
|
$snake = strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1 $2', $string)); |
|
62
|
1 |
|
$sentence = preg_replace('/[-_\s]+/', ' ', $snake); |
|
63
|
1 |
|
if (ctype_upper($string[0])) $sentence = ucfirst($sentence); |
|
64
|
|
|
|
|
65
|
1 |
|
return $sentence; |
|
66
|
|
|
} |
|
67
|
|
|
|