1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Asymptix\helpers; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Strings connected methods. |
7
|
|
|
* |
8
|
|
|
* @category Asymptix PHP Framework |
9
|
|
|
* @author Dmytro Zarezenko <[email protected]> |
10
|
|
|
* @copyright (c) 2015 - 2016, Dmytro Zarezenko |
11
|
|
|
* |
12
|
|
|
* @git https://github.com/Asymptix/Framework |
13
|
|
|
* @license http://opensource.org/licenses/MIT |
14
|
|
|
*/ |
15
|
|
|
class StringHelper { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Returns a string with the first character of str capitalized, if that |
19
|
|
|
* character is alphabetic. |
20
|
|
|
* |
21
|
|
|
* @param string The input string. |
22
|
|
|
* @param string $encoding The encoding parameter is the character encoding. |
23
|
|
|
* If it is omitted, the internal character encoding value will |
24
|
|
|
* be used. |
25
|
|
|
* |
26
|
|
|
* @return string Returns the resulting string. |
27
|
|
|
*/ |
28
|
|
View Code Duplication |
public static function upperCaseFirst($str, $encoding = 'utf8') { |
|
|
|
|
29
|
|
|
if ($encoding) { |
30
|
|
|
$firstLetter = mb_substr(mb_strtoupper($str, $encoding), 0, 1, $encoding); |
31
|
|
|
|
32
|
|
|
return ($firstLetter . mb_substr($str, 1, null, $encoding)); |
33
|
|
|
} else { |
34
|
|
|
$firstLetter = mb_substr(mb_strtoupper($str), 0, 1); |
35
|
|
|
|
36
|
|
|
return ($firstLetter . mb_substr($str, 1)); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns a string with the first character of str lowercased, if that |
42
|
|
|
* character is alphabetic. |
43
|
|
|
* |
44
|
|
|
* @param string The input string. |
45
|
|
|
* @param string $encoding The encoding parameter is the character encoding. |
46
|
|
|
* If it is omitted, the internal character encoding value will |
47
|
|
|
* be used. |
48
|
|
|
* |
49
|
|
|
* @return string Returns the resulting string. |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public static function lowerCaseFirst($str, $encoding = 'utf8') { |
|
|
|
|
52
|
|
|
if ($encoding) { |
53
|
|
|
$firstLetter = mb_substr(mb_strtoupper($str, $encoding), 0, 1, $encoding); |
54
|
|
|
|
55
|
|
|
return ($firstLetter . mb_substr($str, 1, null, $encoding)); |
56
|
|
|
} else { |
57
|
|
|
$firstLetter = mb_substr(mb_strtoupper($str), 0, 1); |
58
|
|
|
|
59
|
|
|
return ($firstLetter . mb_substr($str, 1)); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns a string with the first character of each word in str capitalized, |
65
|
|
|
* if that character is alphabetic. |
66
|
|
|
* |
67
|
|
|
* @param string The input string. |
68
|
|
|
* @param string $encoding The encoding parameter is the character encoding. |
69
|
|
|
* If it is omitted, the internal character encoding value will |
70
|
|
|
* be used. |
71
|
|
|
* |
72
|
|
|
* @return string Returns the resulting string. |
73
|
|
|
*/ |
74
|
|
|
public static function upperCaseWords($str, $encoding = 'utf8') { |
75
|
|
|
if ($encoding) { |
76
|
|
|
return mb_convert_case($str, MB_CASE_TITLE, $encoding); |
77
|
|
|
} else { |
78
|
|
|
return mb_convert_case($str, MB_CASE_TITLE); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns string with all alphabetic characters converted to uppercase. |
84
|
|
|
* |
85
|
|
|
* @param string The input string. |
86
|
|
|
* @param string $encoding The encoding parameter is the character encoding. |
87
|
|
|
* If it is omitted, the internal character encoding value will |
88
|
|
|
* be used. |
89
|
|
|
* |
90
|
|
|
* @return string Returns the resulting string. |
91
|
|
|
*/ |
92
|
|
|
public static function upperCase($str, $encoding = 'utf8') { |
93
|
|
|
if ($encoding) { |
94
|
|
|
return mb_strtoupper($str, $encoding); |
95
|
|
|
} else { |
96
|
|
|
return mb_strtoupper($str); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns string with all alphabetic characters converted to lowercase. |
102
|
|
|
* |
103
|
|
|
* @param string The input string. |
104
|
|
|
* @param string $encoding The encoding parameter is the character encoding. |
105
|
|
|
* If it is omitted, the internal character encoding value will |
106
|
|
|
* be used. |
107
|
|
|
* |
108
|
|
|
* @return string Returns the resulting string. |
109
|
|
|
*/ |
110
|
|
|
public static function lowerCase($str, $encoding = 'utf8') { |
111
|
|
|
if ($encoding) { |
112
|
|
|
return mb_strtolower($str, $encoding); |
113
|
|
|
} else { |
114
|
|
|
return mb_strtolower($str); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.