22
33namespace Larapulse \Support \Handlers ;
44
5+ use Larapulse \Support \Helpers \Regex as RegexHelper ;
6+
57class Str
68{
79 /**
810 * Pop the character off the end of string
911 *
1012 * @param string $str
13+ * @param string $encoding
1114 *
1215 * @return bool|string
1316 */
14- public static function pop (string &$ str )
17+ public static function pop (string &$ str, string $ encoding = null )
1518 {
16- $ last = substr ($ str , -1 );
17- $ str = substr ($ str , 0 , -1 );
19+ $ encoding = $ encoding ?: mb_internal_encoding ();
20+
21+ $ last = mb_substr ($ str , -1 , null , $ encoding );
22+ $ str = mb_substr ($ str , 0 , -1 , $ encoding );
1823
1924 return $ last ;
2025 }
@@ -23,13 +28,16 @@ public static function pop(string &$str)
2328 * Shift a character off the beginning of string
2429 *
2530 * @param string $str
31+ * @param string $encoding
2632 *
2733 * @return bool|string
2834 */
29- public static function shift (string &$ str )
35+ public static function shift (string &$ str, string $ encoding = null )
3036 {
31- $ first = substr ($ str , 0 , 1 );
32- $ str = substr ($ str , 1 );
37+ $ encoding = $ encoding ?: mb_internal_encoding ();
38+
39+ $ first = mb_substr ($ str , 0 , 1 , $ encoding );
40+ $ str = mb_substr ($ str , 1 , null , $ encoding );
3341
3442 return $ first ;
3543 }
@@ -39,24 +47,56 @@ public static function shift(string &$str)
3947 *
4048 * @param string $str
4149 * @param string $subString
50+ * @param bool $repeat
51+ * @param bool $caseSensitive Not working with multi-byte characters
4252 *
4353 * @return string
4454 */
45- public static function cutStart (string $ str , string $ subString = ' ' ) : string
46- {
47- return preg_replace ('/^ ' . preg_quote ($ subString , '/ ' ) . '/ ' , '' , $ str );
55+ public static function cutStart (
56+ string $ str ,
57+ string $ subString = ' ' ,
58+ bool $ repeat = false ,
59+ bool $ caseSensitive = true
60+ ) : string {
61+ $ prepared = RegEx::prepare ($ subString , '/ ' );
62+ $ regex = sprintf (
63+ '/^%s/%s ' ,
64+ ($ subString
65+ ? ($ repeat ? RegexHelper::quantifyGroup ($ prepared , 0 ) : $ prepared )
66+ : ''
67+ ),
68+ (!$ caseSensitive ? 'i ' : '' )
69+ );
70+
71+ return preg_replace ($ regex , '' , $ str );
4872 }
4973
5074 /**
5175 * Cut substring from the end of string
5276 *
5377 * @param string $str
5478 * @param string $subString
79+ * @param bool $repeat
80+ * @param bool $caseSensitive Not working with multi-byte characters
5581 *
5682 * @return string
5783 */
58- public static function cutEnd (string $ str , string $ subString = ' ' ) : string
59- {
60- return preg_replace ('/ ' . preg_quote ($ subString , '/ ' ) . '$/ ' , '' , $ str );
84+ public static function cutEnd (
85+ string $ str ,
86+ string $ subString = ' ' ,
87+ bool $ repeat = false ,
88+ bool $ caseSensitive = true
89+ ) : string {
90+ $ prepared = RegEx::prepare ($ subString , '/ ' );
91+ $ regex = sprintf (
92+ '/%s$/%s ' ,
93+ ($ subString
94+ ? ($ repeat ? RegexHelper::quantifyGroup ($ prepared , 0 ) : $ prepared )
95+ : ''
96+ ),
97+ (!$ caseSensitive ? 'i ' : '' )
98+ );
99+
100+ return preg_replace ($ regex , '' , $ str );
61101 }
62102}
0 commit comments