@@ -356,6 +356,18 @@ public static function is($pattern, $value)
356
356
return false ;
357
357
}
358
358
359
+ /**
360
+ * Determine if a given string is 7 bit ASCII.
361
+ */
362
+ public static function isAscii (string $ value ): bool
363
+ {
364
+ if ($ value == '' ) {
365
+ return true ;
366
+ }
367
+
368
+ return ! preg_match ('/[^\x09\x0A\x0D\x20-\x7E]/ ' , $ value );
369
+ }
370
+
359
371
/**
360
372
* Convert a string to kebab case.
361
373
*
@@ -552,6 +564,14 @@ public static function plural(string $value, int $count = 2): string
552
564
return Pluralizer::plural ($ value , $ count );
553
565
}
554
566
567
+ /**
568
+ * Find the multi-byte safe position of the first occurrence of a given substring in a string.
569
+ */
570
+ public static function position (string $ haystack , string $ needle , int $ offset = 0 , ?string $ encoding = null ): false |int
571
+ {
572
+ return mb_strpos ($ haystack , (string ) $ needle , $ offset , $ encoding );
573
+ }
574
+
555
575
/**
556
576
* Generate a more truly "random" alpha-numeric string.
557
577
*/
@@ -582,6 +602,8 @@ public static function repeat(string $string, int $times)
582
602
583
603
/**
584
604
* Replace a given value in the string sequentially with an array.
605
+ *
606
+ * @param string[] $replace
585
607
*/
586
608
public static function replaceArray (string $ search , array $ replace , string $ subject ): string
587
609
{
@@ -692,6 +714,22 @@ public static function title(string $value): string
692
714
return mb_convert_case ($ value , MB_CASE_TITLE , 'UTF-8 ' );
693
715
}
694
716
717
+ /**
718
+ * Convert the given string to proper case for each word.
719
+ */
720
+ public static function headline (string $ value ): string
721
+ {
722
+ $ parts = explode (' ' , $ value );
723
+
724
+ $ parts = count ($ parts ) > 1
725
+ ? array_map ([static ::class, 'title ' ], $ parts )
726
+ : array_map ([static ::class, 'title ' ], static ::ucsplit (implode ('_ ' , $ parts )));
727
+
728
+ $ collapsed = static ::replace (['- ' , '_ ' , ' ' ], '_ ' , implode ('_ ' , $ parts ));
729
+
730
+ return implode (' ' , array_filter (explode ('_ ' , $ collapsed )));
731
+ }
732
+
695
733
/**
696
734
* Get the singular form of an English word.
697
735
*/
@@ -813,6 +851,7 @@ public static function ucfirst(string $string): string
813
851
*
814
852
* @param int $offset if is negative it starts from the end
815
853
* @param string $replacement default is *
854
+ * @return string
816
855
*/
817
856
public static function mask (string $ string , int $ offset = 0 , int $ length = 0 , string $ replacement = '* ' )
818
857
{
@@ -857,6 +896,9 @@ public static function isUlid($value): bool
857
896
return $ value [0 ] <= '7 ' ;
858
897
}
859
898
899
+ /**
900
+ * Generate a ULID.
901
+ */
860
902
public static function ulid (?DateTimeInterface $ time = null ): Ulid
861
903
{
862
904
if (! class_exists (Ulid::class)) {
@@ -866,6 +908,11 @@ public static function ulid(?DateTimeInterface $time = null): Ulid
866
908
return new Ulid (Ulid::generate ($ time ));
867
909
}
868
910
911
+ /**
912
+ * Determine if a given value is a valid URL.
913
+ *
914
+ * @param string $value
915
+ */
869
916
public static function isUrl ($ value , array $ protocols = []): bool
870
917
{
871
918
if (! is_string ($ value )) {
@@ -916,6 +963,9 @@ public static function isUuid($value): bool
916
963
return preg_match ('/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iD ' , $ value ) > 0 ;
917
964
}
918
965
966
+ /**
967
+ * Generate a UUID (version 4).
968
+ */
919
969
public static function uuid (): UuidInterface
920
970
{
921
971
if (! class_exists (Uuid::class)) {
@@ -925,6 +975,9 @@ public static function uuid(): UuidInterface
925
975
return Uuid::uuid4 ();
926
976
}
927
977
978
+ /**
979
+ * Generate a time-ordered UUID.
980
+ */
928
981
public static function orderedUuid (?DateTimeInterface $ time = null ): UuidInterface
929
982
{
930
983
if (! class_exists (Uuid::class)) {
@@ -934,6 +987,14 @@ public static function orderedUuid(?DateTimeInterface $time = null): UuidInterfa
934
987
return Uuid::uuid7 ($ time );
935
988
}
936
989
990
+ /**
991
+ * Get the smallest possible portion of a string between two given values.
992
+ *
993
+ * @param string $subject
994
+ * @param string $from
995
+ * @param string $to
996
+ * @return string
997
+ */
937
998
public static function betweenFirst ($ subject , $ from , $ to )
938
999
{
939
1000
if ($ from === '' || $ to === '' ) {
@@ -943,6 +1004,9 @@ public static function betweenFirst($subject, $from, $to)
943
1004
return Str::before (Str::after ($ subject , $ from ), $ to );
944
1005
}
945
1006
1007
+ /**
1008
+ * @param string $value
1009
+ */
946
1010
public static function classNamespace ($ value ): string
947
1011
{
948
1012
if ($ pos = strrpos ($ value , '\\' )) {
@@ -952,11 +1016,22 @@ public static function classNamespace($value): string
952
1016
return '' ;
953
1017
}
954
1018
1019
+ /**
1020
+ * Convert the case of a string.
1021
+ */
955
1022
public static function convertCase (string $ string , int $ mode = MB_CASE_FOLD , ?string $ encoding = 'UTF-8 ' ): string
956
1023
{
957
1024
return mb_convert_case ($ string , $ mode , $ encoding );
958
1025
}
959
1026
1027
+ /**
1028
+ * Extracts an excerpt from text that matches the first instance of a phrase.
1029
+ *
1030
+ * @param string $text
1031
+ * @param string $phrase
1032
+ * @param array $options
1033
+ * @return null|string
1034
+ */
960
1035
public static function excerpt ($ text , $ phrase = '' , $ options = [])
961
1036
{
962
1037
$ radius = $ options ['radius ' ] ?? 100 ;
@@ -985,6 +1060,11 @@ public static function excerpt($text, $phrase = '', $options = [])
985
1060
return $ start ->append ($ matches [2 ], $ end )->__toString ();
986
1061
}
987
1062
1063
+ /**
1064
+ * Determine if a given value is valid JSON.
1065
+ *
1066
+ * @param mixed $value
1067
+ */
988
1068
public static function isJson ($ value ): bool
989
1069
{
990
1070
if (! is_string ($ value )) {
@@ -1004,11 +1084,26 @@ public static function isJson($value): bool
1004
1084
return true ;
1005
1085
}
1006
1086
1087
+ /**
1088
+ * Make a string's first character lowercase.
1089
+ *
1090
+ * @param string $string
1091
+ */
1007
1092
public static function lcfirst ($ string ): string
1008
1093
{
1009
1094
return Str::lower (Str::substr ($ string , 0 , 1 )) . Str::substr ($ string , 1 );
1010
1095
}
1011
1096
1097
+ /**
1098
+ * Generate a random, secure password.
1099
+ *
1100
+ * @param int $length
1101
+ * @param bool $letters
1102
+ * @param bool $numbers
1103
+ * @param bool $symbols
1104
+ * @param bool $spaces
1105
+ * @return string
1106
+ */
1012
1107
public static function password ($ length = 32 , $ letters = true , $ numbers = true , $ symbols = true , $ spaces = false )
1013
1108
{
1014
1109
return (new Collection ())
@@ -1032,6 +1127,14 @@ public static function password($length = 32, $letters = true, $numbers = true,
1032
1127
->implode ('' );
1033
1128
}
1034
1129
1130
+ /**
1131
+ * Replace the first occurrence of the given value if it appears at the start of the string.
1132
+ *
1133
+ * @param string $search
1134
+ * @param string $replace
1135
+ * @param string $subject
1136
+ * @return string
1137
+ */
1035
1138
public static function replaceStart ($ search , $ replace , $ subject )
1036
1139
{
1037
1140
$ search = (string ) $ search ;
@@ -1047,6 +1150,14 @@ public static function replaceStart($search, $replace, $subject)
1047
1150
return $ subject ;
1048
1151
}
1049
1152
1153
+ /**
1154
+ * Replace the last occurrence of a given value if it appears at the end of the string.
1155
+ *
1156
+ * @param string $search
1157
+ * @param string $replace
1158
+ * @param string $subject
1159
+ * @return string
1160
+ */
1050
1161
public static function replaceEnd ($ search , $ replace , $ subject )
1051
1162
{
1052
1163
$ search = (string ) $ search ;
@@ -1080,6 +1191,9 @@ public static function replaceMatches($pattern, $replace, $subject, $limit = -1)
1080
1191
return preg_replace ($ pattern , $ replace , $ subject , $ limit );
1081
1192
}
1082
1193
1194
+ /**
1195
+ * @param string $value
1196
+ */
1083
1197
public static function reverse ($ value ): string
1084
1198
{
1085
1199
return implode (array_reverse (mb_str_split ($ value )));
@@ -1133,11 +1247,25 @@ public static function rtrim($value, $charlist = null)
1133
1247
return rtrim ($ value , $ charlist );
1134
1248
}
1135
1249
1250
+ /**
1251
+ * Remove all "extra" blank space from the given string.
1252
+ *
1253
+ * @param string $value
1254
+ */
1136
1255
public static function squish ($ value ): null |array |string
1137
1256
{
1138
1257
return preg_replace ('~(\s|\x{3164}|\x{1160})+~u ' , ' ' , static ::trim ($ value ));
1139
1258
}
1140
1259
1260
+ /**
1261
+ * Replace text within a portion of a string.
1262
+ *
1263
+ * @param string|string[] $string
1264
+ * @param string|string[] $replace
1265
+ * @param int|int[] $offset
1266
+ * @param null|int|int[] $length
1267
+ * @return string|string[]
1268
+ */
1141
1269
public static function substrReplace ($ string , $ replace , $ offset = 0 , $ length = null ): array |string
1142
1270
{
1143
1271
if ($ length === null ) {
@@ -1147,26 +1275,100 @@ public static function substrReplace($string, $replace, $offset = 0, $length = n
1147
1275
return substr_replace ($ string , $ replace , $ offset , $ length );
1148
1276
}
1149
1277
1278
+ /**
1279
+ * Swap multiple keywords in a string with other keywords.
1280
+ *
1281
+ * @param string $subject
1282
+ * @return string
1283
+ */
1150
1284
public static function swap (array $ map , $ subject ): array |string
1151
1285
{
1152
1286
return str_replace (array_keys ($ map ), array_values ($ map ), $ subject );
1153
1287
}
1154
1288
1289
+ /**
1290
+ * Take the first or last {$limit} characters of a string.
1291
+ */
1292
+ public static function take (string $ string , int $ limit ): string
1293
+ {
1294
+ if ($ limit < 0 ) {
1295
+ return static ::substr ($ string , $ limit );
1296
+ }
1297
+
1298
+ return static ::substr ($ string , 0 , $ limit );
1299
+ }
1300
+
1301
+ /**
1302
+ * Convert the given string to Base64 encoding.
1303
+ *
1304
+ * @param string $string
1305
+ */
1306
+ public static function toBase64 ($ string ): string
1307
+ {
1308
+ return base64_encode ($ string );
1309
+ }
1310
+
1311
+ /**
1312
+ * Split a string into pieces by uppercase characters.
1313
+ *
1314
+ * @param string $string
1315
+ * @return bool|string[]
1316
+ */
1155
1317
public static function ucsplit ($ string ): array |bool
1156
1318
{
1157
1319
return preg_split ('/(?=\p{Lu})/u ' , $ string , -1 , PREG_SPLIT_NO_EMPTY );
1158
1320
}
1159
1321
1322
+ /**
1323
+ * Unwrap the string with the given strings.
1324
+ *
1325
+ * @param string $value
1326
+ * @param string $before
1327
+ * @param null|string $after
1328
+ */
1329
+ public static function unwrap ($ value , $ before , $ after = null ): string
1330
+ {
1331
+ if (static ::startsWith ($ value , $ before )) {
1332
+ $ value = static ::substr ($ value , static ::length ($ before ));
1333
+ }
1334
+
1335
+ if (static ::endsWith ($ value , $ after ??= $ before )) {
1336
+ $ value = static ::substr ($ value , 0 , -static ::length ($ after ));
1337
+ }
1338
+
1339
+ return $ value ;
1340
+ }
1341
+
1342
+ /**
1343
+ * Get the number of words a string contains.
1344
+ *
1345
+ * @param string $string
1346
+ */
1160
1347
public static function wordCount ($ string ): array |int
1161
1348
{
1162
1349
return str_word_count ($ string );
1163
1350
}
1164
1351
1352
+ /**
1353
+ * Wrap the string with the given strings.
1354
+ *
1355
+ * @param string $value
1356
+ * @param string $before
1357
+ * @param null|string $after
1358
+ */
1165
1359
public static function wrap ($ value , $ before , $ after = null ): string
1166
1360
{
1167
1361
return $ before . $ value . ($ after ??= $ before );
1168
1362
}
1169
1363
1364
+ /**
1365
+ * Wrap a string to a given number of characters.
1366
+ *
1367
+ * @param string $string
1368
+ * @param int $characters
1369
+ * @param string $break
1370
+ * @param bool $cutLongWords
1371
+ */
1170
1372
public static function wordWrap ($ string , $ characters = 75 , $ break = "\n" , $ cutLongWords = false ): string
1171
1373
{
1172
1374
return wordwrap ($ string , $ characters , $ break , $ cutLongWords );
0 commit comments