-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertString.php
66 lines (60 loc) · 1.51 KB
/
convertString.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* Convert from Title Case to Slug String.
*
* For eg:
*
* array (
* 'firstKey' => 'Value of key',
* 'secondKey' => 'Value of key',
* 'firstKey' => array(
* 'subFirstKey' =>'Value of key',
* 'subSecondKey' => array (
* 'subSubFirstKey' = > ' Value of key ',
* ),
* ),
* 'thirdKey' => 'Value of first key',
* )
*
* will be
*
* array (
* 'first-key' =>
* array (
* 'sub-first-key' => 'Value of key',
* 'sub-second-key' =>
* array (
* 'sub-sub-first-key' => ' Value of key ',
* ),
* ),
* 'second-key' => 'Value of key',
* 'third-key' => 'Value of first key',
* )
*/
function convertFromTitleCaseToSlug($text)
{
return strtolower(preg_replace('#([a-zA-Z])(?=[A-Z])#', '$1-', $text));
}
//pass file param to $file variable
$file = $argv[1];
//pass array from file to $arr variable
$arr = include $file;
$newArr = [];
//loop through the array, convert the key that contains TitleCase to dash as a delim
function convertArray($arr)
{
$newArr = [];
foreach ($arr as $key => $value) {
$newKey = convertFromTitleCaseToSlug($key);
if (is_array($value)) {
$newArr += [$newKey => convertArray($value)];
} else {
$newArr += [$newKey => $value];
}
}
return $newArr;
}
//save to output.txt
$newArr = convertArray($arr);
$output = "<?php\n" . "return " . var_export($newArr, true) . ";";
file_put_contents('output.txt', $output);