https://github.com/awssat/str-helper
⚡️ A flexible & powerful string manipulation helper for PHP | using pipe method chaining
https://github.com/awssat/str-helper
chainable-methods composer-package flexible method-chaining packagist php pipe str str-helper string-manipulation
Last synced: 12 days ago
JSON representation
⚡️ A flexible & powerful string manipulation helper for PHP | using pipe method chaining
- Host: GitHub
- URL: https://github.com/awssat/str-helper
- Owner: awssat
- License: mit
- Created: 2017-11-19T20:34:22.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-03T19:59:20.000Z (over 6 years ago)
- Last Synced: 2025-03-24T12:47:47.957Z (29 days ago)
- Topics: chainable-methods, composer-package, flexible, method-chaining, packagist, php, pipe, str, str-helper, string-manipulation
- Language: PHP
- Homepage:
- Size: 41 KB
- Stars: 44
- Watchers: 1
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# str-helper
[](https://packagist.org/packages/awssat/str-helper)
[](https://styleci.io/repos/111329905)
[](https://travis-ci.org/awssat/str-helper)⚡️ A flexible, simple & yet powerful string manipulation helper for PHP. It gives you the magic of method chaining and it's easier and shorter to be included in views. It Supports most of [PHP built-in strings functions](http://php.net/manual/en/book.strings.php) (and other useful methods like: contains, equal, append, prepend ...).
```php
str('Hi World')->replace(' ', '+')->lower();
```## Why use this instead of other string maniplulation packages ?
This is a wrapper for PHP default string functions, to provide a very poweful method chaining and conditions.
You don't have to learn new methods names, just use PHP functions names that you know.## Install/Use
You can install the package via composer locally in your project folder:```bash
$ composer require awssat/str-helper
```After installing it, just start using `StrHelper` class, or simply the helper function `str()`:
## Examples
```bash
str('Hi Hello')->strReplace(' ', '-');
>> hi-hello
``````bash
str('Hi Hello')->prepend('[')->append(']');
>> [Hi Hello]
```In case you want an explicit string value for conditions, use "get":
```bash
if(str('Hi')->lower->get() == 'hi') {
echo 'yes';
}
>> yes
```There is a "tap" method:
```bash
str('LINK.COM')->tap(function($v){ print($v); })->lower();
>> LINK.COM
```for callbacks use "do" method:
```bash
str('link.com')->do(function($string){
return strip_tags($string);
});
>> link.com
```
or:
```bash
str('link.com')->do(function(){
$this->stripTags();
});
>> link.com
```you may notice using camelCase instead of snake_case for method name works too.
You can also use conditions, if(..), else(), endif()
```php
str('hi')
->ifContains('hi')
->upper();
>> HI
```if can take an anonymous function
```php
str('hi')
->if(function(){
return $this->contains('hi');
})
->upper();
>> HI
```All methods are availabe to be called statically, as:
```php
StrHelper::capitalize('life');
```
or using str() function.
```php
str()::capitalize('nomad');
```__[UTF-8 Support] If mbstring library is installed and you call a strpos function, mb_strpos will be called instead.__
## Tests
Simply use:
```bash
$ composer test
```
## Credits
- [Abdulrahman M.](https://github.com/abdumu)
- [All Contributors](../../contributors)## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.