loongs / helper
loong-swoole helpers — multibyte-safe static string utilities (Str)
dev-main
2026-09-30 06:09 UTC
Requires
- php: ^8.4
- ext-mbstring: *
Requires (Dev)
None
Suggests
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-30 06:17:25 UTC
README
Small, dependency-free helpers for loong-swoole (PHP 8.4). Starts with Loongs\Helper\Str:
static, UTF-8 / multibyte-safe string utilities.
composer require loongs/helper
use Loongs\Helper\Str; Str::camel('user_profile-id'); // userProfileId Str::snake('UserProfile'); // user_profile Str::slug('你好 世界 Hello'); // ni-hao-shi-jie-hello (ext-intl transliteration) Str::slug('你好 世界', '-', false); // 你好-世界 (keep Unicode) Str::limit('你好世界和平', 4); // 你好世界... (characters, not bytes) Str::mask('13812345678', '*', 3, 4); // 138****5678 Str::padBoth('中', 5, '*'); // **中** Str::is('user.*', 'user.stats'); // true Str::uuid(); // 3f0c…-4… (v4, random_bytes) Str::ulid(); // 01J… (26 chars, time-sortable)
Methods
| Group | Methods |
|---|---|
| Case | camel, studly / pascal, snake($v, $delimiter = '_'), kebab, lower, upper, title, ucfirst, lcfirst |
| ASCII / URL | ascii, isAscii, slug($title, $separator = '-', $ascii = true) |
| Inspect | startsWith, endsWith, contains($h, $needles, $ignoreCase = false), containsAll, is($pattern, $value, $ignoreCase = false) (wildcard *, array of patterns), isJson, isUuid, isUlid, length, wordCount |
| Slice | substr, limit($v, $limit = 100, $end = '...'), words($v, $words = 100, $end = '...'), before, beforeLast, after, afterLast, between, betweenFirst |
| Build | replaceFirst, replaceLast, start, finish, wrap, mask, padLeft, padRight, padBoth, repeat, reverse |
| Whitespace | trim, ltrim, rtrim (Unicode whitespace such as U+3000 by default), squish |
| Generate | random($length = 16) (secure, [A-Za-z0-9]), uuid() (v4), ulid(?int $timeMs = null) |
| Cache | flushCache(), cacheSizes() |
startsWith/endsWith/containsaccept a string or an iterable of needles; empty needles never match.limit,substr,length,mask,pad*,reversecount characters (UTF-8), not bytes.camel,snake,studlyresults are memoised per process; each cache is reset afterStr::CACHE_LIMIT(1024) entries, so long-running Swoole workers stay bounded.ascii/slugtransliterate with ext-intl (Any-Latin; Latin-ASCII, CJK → pinyin), fall back to iconv//TRANSLIT, and drop anything still non-ASCII.- No pluralisation / inflection on purpose.
Requirements
PHP ^8.4 (uses mb_trim, mb_str_pad, json_validate), ext-mbstring. Optional: ext-intl, ext-iconv.
License
MIT