Skip to content

Commit 2c71964

Browse files
committed
add new string help methods
1 parent 80b4ea2 commit 2c71964

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/Str/StringHelper.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
use function base64_encode;
2323
use function count;
2424
use function crc32;
25+
use function escapeshellarg;
2526
use function gethostname;
2627
use function hash;
2728
use function hex2bin;
2829
use function is_int;
2930
use function is_string;
3031
use function mb_strwidth;
3132
use function microtime;
33+
use function preg_match;
3234
use function preg_split;
3335
use function random_bytes;
3436
use function random_int;
@@ -38,6 +40,7 @@
3840
use function str_word_count;
3941
use function strlen;
4042
use function strpos;
43+
use function strtr;
4144
use function substr;
4245
use function trim;
4346
use function uniqid;
@@ -375,6 +378,17 @@ public static function deleteStripSpace($fileName, $type = 0)
375378
return preg_replace($preg_arr, '', $data);
376379
}
377380

381+
/**
382+
* @param string $template
383+
* @param array $vars
384+
*
385+
* @return string
386+
*/
387+
public static function replaces(string $template, array $vars): string
388+
{
389+
return strtr($template, $vars);
390+
}
391+
378392
////////////////////////////////////////////////////////////
379393
/// Other
380394
////////////////////////////////////////////////////////////
@@ -399,6 +413,18 @@ public static function utf8WordCount(string $str): int
399413
return count(preg_split('~[^\p{L}\p{N}\']+~u', $str));
400414
}
401415

416+
/**
417+
* Escapes a token through escape shell arg if it contains unsafe chars.
418+
*
419+
* @param string $token
420+
*
421+
* @return string
422+
*/
423+
public static function escapeToken(string $token): string
424+
{
425+
return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);
426+
}
427+
402428
/**
403429
* @param string $arg
404430
*

0 commit comments

Comments
 (0)