Skip to content

⚡️ A flexible & powerful string manipulation helper for PHP | using pipe method chaining

License

Notifications You must be signed in to change notification settings

awssat/str-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

str-helper

Latest Version on Packagist StyleCI Build Status

⚡️ 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 (and other useful methods like: contains, equal, append, prepend ...).

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:

$ composer require awssat/str-helper

After installing it, just start using StrHelper class, or simply the helper function str():

Examples

str('Hi Hello')->strReplace(' ', '-');
>> hi-hello
str('Hi Hello')->prepend('[')->append(']');
>> [Hi Hello]

In case you want an explicit string value for conditions, use "get":

if(str('Hi')->lower->get() == 'hi') {
        echo 'yes'; 
}
>> yes

There is a "tap" method:

str('LINK.COM')->tap(function($v){ print($v); })->lower();
>> LINK.COM

for callbacks use "do" method:

str('<a>link.com</a>')->do(function($string){ 
        return strip_tags($string); 
});
>> link.com

or:

str('<a>link.com</a>')->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()

str('<html>hi</html>')
            ->ifContains('hi')
            ->upper();
>> <HTML>HI</HTML>       

if can take an anonymous function

str('<html>hi</html>')
            ->if(function(){
                    return $this->contains('hi');
            })
            ->upper();
>> <HTML>HI</HTML>       

All methods are availabe to be called statically, as:

StrHelper::capitalize('life');

or using str() function.

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:

$ composer test

Credits

License

The MIT License (MIT). Please see License File for more information.