-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringUtils.php
112 lines (102 loc) · 3.54 KB
/
StringUtils.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace Scandio\lmvc\utils\string;
/**
* Static library for string functions
*/
class StringUtils
{
/**
* Converts a camel cased string to a lower cased string with a delimiter
*
* @param string $camelCasedString the input string as a camelCasedString
* @param string $delimiter optional the delimiter the default is "-"
* @return string the result is a camel-cased-string instead of a camelCasedString
*/
public static function camelCaseTo($camelCasedString, $delimiter = '-')
{
return strtolower(preg_replace('/(?<=\\w)(?=[A-Z])/', $delimiter . "$1", $camelCasedString));
}
/**
* Converts a delimited string to a camel cased string
*
* @param string $otherString the input string a delimited-string
* @param string $delimiter optional the delimiter the default is "-"
* @return string the result is a delimitedString instead of a delimited-string
*/
public static function camelCaseFrom($otherString, $delimiter = '-')
{
return lcfirst(implode('', array_map(function ($data) {
return ucfirst($data);
}, explode($delimiter, $otherString))));
}
/**
* Counts the number of bytes which a string is long. In other words: returns
* the string's byte-length.
*
* @param $string which bytes shall be counted
* @return int number of bytes in string
*/
public static function bytes($string)
{
$strlen_var = strlen($string);
$d = 0;
for($c = 0; $c < $strlen_var; ++$c){
$ord_var_c = ord($string{$c});
switch(true){
case(($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
// characters U-00000000 - U-0000007F (same as ASCII)
$d++;
break;
case(($ord_var_c & 0xE0) == 0xC0):
// characters U-00000080 - U-000007FF, mask 110XXXXX
$d+=2;
break;
case(($ord_var_c & 0xF0) == 0xE0):
// characters U-00000800 - U-0000FFFF, mask 1110XXXX
$d+=3;
break;
case(($ord_var_c & 0xF8) == 0xF0):
// characters U-00010000 - U-001FFFFF, mask 11110XXX
$d+=4;
break;
case(($ord_var_c & 0xFC) == 0xF8):
// characters U-00200000 - U-03FFFFFF, mask 111110XX
$d+=5;
break;
case(($ord_var_c & 0xFE) == 0xFC):
// characters U-04000000 - U-7FFFFFFF, mask 1111110X
$d+=6;
break;
default:
$d++;
};
};
return $d;
}
/**
* Finds out if a string starts with a given substring.
*
* @param $haystack the bigger part
* @param $needle to subpart
* @return bool indicating if string starts with $needle
*/
public static function starts($haystack, $needle)
{
return !strncmp($haystack, $needle, strlen($needle));
}
/**
* Finds out if a string ends with a given substring.
*
* @param $haystack the bigger part
* @param $needle to subpart
* @return bool indicating if string starts with $needle
*/
public static function ends($haystack, $needle)
{
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
}