robertaodj/str
Composer 安装命令:
composer require robertaodj/str
包简介
A modern PHP string manipulation library with fluent interface and extensive functionality
README 文档
README
A modern, fluent PHP library for string manipulation with extensive functionality inspired by Laravel's Str class and enhanced with additional features for Brazilian Portuguese development.
🇧🇷 Versão em Português | 📖 English Documentation
Features
- 🔄 Fluent Interface: Chain methods for readable and maintainable code
- 🌍 Internationalization: Built-in support for Portuguese and English
- 🚀 Modern PHP: Requires PHP 8.2+ with full type declarations
- 📱 Brazilian Helpers: CPF, CNPJ, phone formatting and validation
- 🔧 Laravel-inspired: Methods compatible with Laravel's Str class
- 🧪 Well-tested: Comprehensive test suite
- 📚 Well-documented: Extensive documentation with examples
📖 When to use get() and when it's optional
✅ NO need for get() (automatic conversion):
- Echo and print:
echo Str::of('hello')->upper() - Concatenation:
"Result: " . Str::of('test')->title() - Interpolation:
"Value: $str" - Comparisons:
if ($str == 'expected') - Direct assignments:
$var = Str::of('test')->lower()
⚠️ Needs explicit cast (string):
- PHP functions:
strlen((string) $str) - PHP functions:
empty((string) $str) - Arrays:
['key' => (string) $str] - JSON:
json_encode(['key' => (string) $str])
🔧 Always works with get():
- When in doubt, use
.get()at the end - To ensure full compatibility
- In contexts where string type is mandatory
Installation
composer require robertaodj/str
Quick Start
use Robertaodj\Str\Str; // Create a string instance $result = Str::of(" Hello World! ") ->trim() ->lower() ->studly() ->get(); // "HelloWorld!" // Static methods echo Str::slug("Olá Mundo"); // "ola-mundo" echo Str::kebab("HelloWorld"); // "hello-world" echo Str::mask("12345678901", "###.###.###-##"); // "123.456.789-01"
Examples
Basic String Manipulation
use Robertaodj\Str\Str; // Text cleaning and formatting $text = Str::of(" Texto com acentos e símbolos!@# ") ->trim() ->removeAccents() ->alphanumeric(' ') ->title() ->get(); // "Texto Com Acentos E Simbolos" // Case conversions echo Str::camel("hello-world"); // "helloWorld" echo Str::snake("HelloWorld"); // "hello_world" echo Str::studly("hello_world"); // "HelloWorld"
Brazilian Portuguese Features
// Document formatting echo Str::mask("12345678901", "cpf"); // "123.456.789-01" echo Str::mask("12345678000195", "cnpj"); // "12.345.678/0001-95" // Capitalization with Portuguese rules echo Str::of("o gato subiu no telhado da casa") ->capitalize('pt') ->get(); // "O Gato Subiu no Telhado da Casa"
Advanced Features
// Content validation $html = "<p>Hello <b>World</b></p>"; echo Str::of($html)->isHtml()->get(); // true echo Str::of($html)->stripTags()->get(); // "Hello World" // Text truncation $longText = "Lorem ipsum dolor sit amet..."; echo Str::of($longText)->limit(10)->get(); // "Lorem ipsu..." echo Str::of($longText)->words(3)->get(); // "Lorem ipsum dolor..."
Available Methods
Validation Methods
isEmail()- Check if string is a valid emailisHtml()- Check if string contains HTML tagsisJson()- Check if string is valid JSONisXml()- Check if string is valid XMLisSerialized()- Check if string is serializedisUrl()- Check if string is a valid URLisUuid()- Check if string is a valid UUID
Transformation Methods
camel()- Convert to camelCasestudly()- Convert to StudlyCasesnake()- Convert to snake_casekebab()- Convert to kebab-caseslug()- Create URL-friendly slugtitle()- Convert to Title Caseupper()- Convert to UPPERCASElower()- Convert to lowercase
Filtering Methods
alphanumeric()- Keep only alphanumeric charactersnumeric()- Keep only numbersalpha()- Keep only lettersremoveAccents()- Remove accents and diacriticsstripTags()- Remove HTML/XML tags
Formatting Methods
mask()- Apply formatting masks (CPF, CNPJ, phone, etc.)pad()- Pad string to specified lengthtrim()- Remove whitespace from both endstruncate()- Truncate to specified lengthwords()- Limit to specified number of words
Documentation
Testing
# Run tests composer test # Run tests with coverage composer test-coverage # Fix code style composer cs-fix # Run static analysis composer phpstan
Contributing
Please see CONTRIBUTING.md for details.
Security
If you discover any security related issues, please email security@robertaodj.com instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.
Credits
- Robert D Jesus
- Based on Simpla Components Str
- Inspired by Laravel's Str class
- All Contributors
robertaodj/str 适用场景与选型建议
robertaodj/str 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 47 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 08 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「fluent」 「string」 「helper」 「manipulation」 「php8」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 robertaodj/str 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 robertaodj/str 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 robertaodj/str 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
Encode integer IDs using a preset or customized symbol set
Presentation and Formatting helper with nice fluent interface.
Datetime helpers for timezone handling
base62 encoder and decoder also for big numbers with Laravel integration
PHP string processing library.
统计信息
- 总下载量: 47
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-02