paquettg/string-encode
Flexible PHP string encoding helper for multibyte text. Convert strings safely between encodings (UTF-8 by default) with a fluent API, plus utilities for encoding-aware regex handling. Supports PHP 7.2–7.4.
convert()->fromString()->toString()) aligns with Laravel’s expressive syntax (e.g., Str::of(), Validator::extend()), reducing cognitive load for developers. It bridges Laravel’s Str helper and mb_* functions, offering a higher-level abstraction for multibyte string operations.trans(), locale(), or localization packages like spatie/laravel-translatable). Ensures consistent UTF-8 handling across user-generated content, APIs, and database layers.Str helper, which lacks native mb_regex functionality. Useful for:
Str::contains() with non-ASCII chars).Form Requests for emails with Unicode domains).Encoder class can be seamlessly injected into Laravel’s IoC container, enabling dependency injection in controllers, commands, or services. Example:
public function __construct(private Encoder $encoder) {}
StringEncoder facade to mirror Laravel’s Str or Cache facades, improving developer ergonomics.DB::select("SELECT mb_convert_encoding(column, 'UTF-8') FROM table");
public function getTitleAttribute($value) {
return $this->encoder->convert()->fromString($value)->toUTF8();
}
Validator to add encoding-specific rules (e.g., Rule::encoding('UTF-8')), reducing boilerplate in Form Requests.mb_* function changes in PHP 8.1+ (e.g., stricter type handling).symfony/polyfill-mbstring).mb_regex is slower than PCRE (preg_*). Benchmark in high-traffic routes (e.g., API endpoints) to avoid latency spikes.Str::of($str)->cache()) or use lazy loading.mb_detect_encoding() is unreliable for mixed-encoding strings. The package may fail silently or throw warnings.mb_check_encoding() or manual fallback logic:
$encoding = mb_detect_encoding($str, ['UTF-8', 'ISO-8859-1'], true);
removeBOM option could interfere with Laravel’s file storage (e.g., uploaded files with UTF-8 BOMs).Storage::put() with explicit encoding flags.shlinkio/string-encoder) or maintain a lightweight patch set.mb_regex compare to Laravel’s Str::of()->contains() for multibyte strings in bulk operations (e.g., 10K+ records)? Test with:
$time = microtime(true);
for ($i = 0; $i < 10000; $i++) {
$encoder->convert()->fromString($mbString)->toUTF8();
}
echo microtime(true) - $time;
mbstring is disabled? (Laravel requires it for Str::of().)mb_convert_encoding() with unsupported chars like �.)@{{ $var }} with multibyte vars)?trans() with encoded strings)?Validator, Str, or trans())?mb_regex and file I/O operations?mb_* functions or iconv suffice for 80% of use cases? When does this package add value?symfony/polyfill-iconv or vlucas/phpdotenv (for encoding-aware env files).mb_* calls with the package’s fluent API (e.g., $encoder->fromString($str)->toAscii() vs. mb_convert_encoding($str, 'ASCII')).Form Requests:
use Illuminate\Validation\Rule;
Rule::encoding('UTF-8')->message('The :attribute must be UTF-8 encoded.');
trans()) are consistently encoded when stored/retrieved in databases or caches.class UTF8Scope implements \Illuminate\Database\Eloquent\Scope {
public function apply(\Illuminate\Database\Eloquent\Builder $builder, \Illuminate\Database\Eloquent\Model $model) {
$builder->selectRaw("CONVERT(`column` USING utf8mb4) as `column`");
}
}
Request::input() with multibyte chars).Response::json($data, 200, [], JSON_UNESCAPED_UNICODE)).Str::of($filename)->toAscii() for URLs).removeBOM for text files).Phase 1: Proof of Concept (1–2 Days)
composer require paquettg/string-encode.Route::get('/encode-test', function () {
$str = "Café 🚀";
$encoder = new \StringEncoder\Encoder();
return $encoder->convert()->fromString($str)->toUTF8();
});
mb_regex.Phase 2: Core Integration (3–5 Days)
Encoder as a singleton in AppServiceProvider:
$this->
How can I help you explore Laravel packages today?