String Helpers
This library contains some basic php helpers that will make manipulating strings easier and better.
Installation
composer require aphp/string-helpers ~1.1.0
Below is the list of string helpers this package provides.
use aphp\Helpers\str
- str::contains($haystack, $needle, $caseInsensitive = false): checks if the $needle string is present in $haystack string and returns true or false accordingly. When $casInsensitive is true the check will be as the name says, case insensitive.
$fullName = 'John Joseph Doe';
$fatherName = 'Joseph';
str::contains($fullName, $fatherName); // *true*
$fatherName = 'joseph';
str::contains($fullName, $fatherName); // *false*
str::contains($fullName, $fatherName, true); // *true*
- str::equals($string1, $string2, $caseInsensitive = false): checks if the given string are equal. When $caseInsensitive is true the check will be case insensitive.
$string1 = 'In the bleak mid-winter';
$string2 = 'In the bleak mid-winter';
str::equals($string1, $string2); // *true*
$string2 = 'In the bleak MID-WINTER';
str::equals($string1, $string2); // *false*
str::equals($string1, $string2, true); // *true*
- str::starts_with($haystack, $needle, $caseInsensitive = false): checks if the given $haystack string starts with the given $needle. $caseInsensitive = true for case insensitive check.
$fullName = 'John Joseph Doe';
$firstName = 'John';
str::starts_with($fullName, $firstName); // *true*
$firstName = 'john';
str::starts_with($fullName, $firstName); // *false*
str::starts_with($fullName, $firstName, true); // *true*
$firstName = 'joseph';
str::starts_with($fullName, $firstName); // *false*
str::starts_with($fullName, $firstName, true); // *false*
- str::ends_with($haystack, $needle, $caseInsensitive = false): checks if the given $haystack string ends with the given $needle. $caseInsensitive = true for case insensitive check.
$fullName = 'John Joseph Doe';
$lastName = 'Doe';
str::ends_with($fullName, $lastName); // *true*
$lastName = 'doe';
str::ends_with($fullName, $lastName); // *false*
str::ends_with($fullName, $lastName, true); // *true*
$lastName = 'joseph';
str::ends_with($fullName, $lastName); // *false*
str::ends_with($fullName, $lastName, true); // *false*
- str::limit($string, $length, $suffix = '...'): this function limits/splice the $string at the given $length and suffixes it with the given $suffix.
$title = 'This is a very long string that cannot be displayed in the list section of your blog, so you have to limit it.';
str::limit($title, 20); // *This is a very long ...*
str::limit($title, 20, '---'); // *This is a very long ---*
- str::after($haystack, $needle, bool $caseInsensitive = false): returns the string after $needle from $haystack. When $caseSensitive = true, $needle will be searched case insensitively.
$title = 'This is a very long string that cannot be displayed in the list section of your blog, so you have to break it.';
str::after($title, 'This is a very long string that cannot be displayed'); // *in the list section of your blog, so you have to break it.*
str::after($title, 'Yolo'); // *''*
str::after($title, 'this'); // *''*
str::after($title, 'this', true); // *is a very long string that cannot be displayed in the list section of your blog, so you have to break it.*
- str::before($haystack, $needle, bool $caseInsensitive = false): returns the string before $needle from $haystack. When $caseSensitive = true, $needle will be searched case insensitively.
$title = 'This is a very long string that cannot be displayed in the list section of your blog, so you have to break it.';
str::before($title, 'a very long'); // *This is *
str::before($title, 'Yolo'); // *''*
str::before($title, 'this'); // *''*
str::before($title, 'this', true); // *''*
str::before($title, 'is a very', true); // *This *
- str::between(string $haystack, string $from, string $to, bool $caseInsensitive = false): returns the string found between the given $from sub-string and $to sub-string from the given $haystack. When $caseInsensitive = true, $from and $to will be searched case insensitively.
$title = 'This is a very long string that cannot be displayed in the list section of your blog, so you have to break it.';
str::between($title, 'This', 'long'); // * is a very *
str::between($title, 'is', 'This'); // *''*
str::between($title, 'is', 'This', true); // *''*
str::between($title, 'this', 'is', true); // *' is a very '*
str::between($title, 'sO YoU hAve', 'it', true); // *' to break '*
- str::strip_special_chars(string $string, string $replaceWith = ''): strips the given $string of any special characters and returns a string containing only alphabets, or numbers. Passing $replaceWith will replace the special chars in the string with $replaceWith.
$title = '^&%This*&(*is)(^%^#@_=0it.';
str::strip_special_chars($title); // *Thisis0it*
str::strip_special_chars($title, '-'); // *---This----is---------0it-*
- str::camel_case($string): strips the given $string of any special character and spaces, and turns $string into "CamelCase".
$title = 'This is a normal string';
str::camel_case($title); // *thisIsANormalString*
$title = 'This is$a#normal%string';
str::camel_case($title); // *thisIsANormalString*
- str::studly_case($string): strips the given $string of any special character and spaces, and turns it into "studlyCase".
$title = 'This is a normal string';
str::studly_case($title); // *ThisIsANormalString*
$title = 'This is$a#normal%string';
str::studly_case($title); // *ThisIsANormalString*
- str::kebab_case($string): strips the given $string of any special character and spaces, and turns it into "kebab-case".
$title = 'This is a normal string';
str::kebab_case($title); // *this-is-a-normal-string*
$title = 'This is$a#normal%string';
str::kebab_case($title); // *this-is-a-normal-string*
- str::snake_case($string): strips the given $string of any special character and spaces, and turns it into "snake_case". strips the given $string of any special character and spaces, and turns it into "kebab-case".
$title = 'This is a normal string';
str::snake_case($title); // *this_is_a_normal_string*
$title = 'This is$a#normal%string';
str::snake_case($title); // *this_is_a_normal_string*
- str::strip_numbers($string): removes any numeric values found in $string. strips the given $string of any special character and spaces, and turns it into "kebab-case".
$title = 'great here is my contact no: 12346732';
str::strip_numbers($title); // *great here is my contact no:*
- str::chunks($string, $length = 100): breaks the given $string at every $length, and returns an array of string each containing string of $length.
$title = 'hope ya'll like this small helper library';
str::chunk($title, 10); // *['hope ya'll', ' like this', ' small hel', 'per librar', 'y']:*
Test running
- install phpunit, composer, php, mingw64 if not installed
- composer install at package dir
- composer run-script test
OR
- composer run-script install_phpunit5
- composer run-script test5
🔵 Useful links
- Composer package generator
- Cmd windows
- PHP downloads
- PHP installations
- Git client
More features
For more features:
- Read source code and examples
- Practice with
String Helpers
in real code