Is a simple library that provides common string functions not natively provided by PHP.
- You can install via composer.
composer require jameslevi/string
- If you are not using any PHP framework, just paste this code at the upper part of your code to include composer autoload mechanism in your project.
<?php
if(file_exists(__DIR__.'/vendor/autoload.php'))
{
require __DIR__.'/vendor/autoload.php';
}
- You can now use all functions included from this library.
Make comparison between two strings.
// Returns true because "hello" matches one of the word inside array.
str_equals("hello", array(
"hello",
"world",
))
Determine if string starts with the match string.
str_starts_with("hello", "h") // Returns true because hello starts with an h.
Determine if string ends with the match string.
// Returns false because @email.com is not included from the array.
str_ends_with("foo@email.com", array(
"@gmail.com",
"@yahoo.com",
))
Remove all matched character from the string.
str_remove("hello world", "l") // Returns "heo word".
Remove one or more characters from one or both side of the string.
str_move("hello world", 1, 2) // Returns "ello wor".
Remove one or more characters from the left side of the string.
str_move_left("hello world", 3) // Returns "lo world".
Remove one or more characters from the right side of the string.
str_move_right("hello world", 2) // Returns "hello wor".
Count the number of occurences of numeric characters in string.
str_count_numeric("hello world 123") // Returns "3".
Count the number of occurences of uppercase letters in string.
str_count_uppercase_letter("Hello World") // Returns "2".
Count the number of occurences of lowercase letters in string.
str_count_lowercase_letter("Hello World") // Returns "8".
Count the number of occurences of letters in string.
str_count_letter("Hello World") // Returns "10".
Count the number of lines in string.
str_count_line("Hello World") // Returns "1".
Count the number of spaces in string.
str_count_spaces("Hello World") // Returns "1".
Count the number of special characters in string.
str_count_special_chars("Hello World!!!") // Returns "3".
Count the number of words in a string.
str_count_words("Hello World") // Returns "2".
Return list of words from the string.
str_words("Hello World")
This function will return
array(2)
(
[0] => "Hello"
[1] => "World"
)
Test if string contains one or more word or characters.
str_contains("Hello World", "Hello") // Returns true.
Break a string into just two segments.
str_break("Hello World", " ")
This function will return
array(2)
(
[0] => "Hello"
[1] => "World"
)
Determine if string is in uppercase.
str_is_upper("hello world") // Returns false.
str_is_upper("Hello World") // Returns false.
str_is_upper("HELLO WORLD") // Returns true.
Determine if string is in lowercase.
str_is_lower("hello world") // Returns true.
str_is_lower("Hello World") // Returns false.
str_is_lower("HELLO WORLD") // Returns false.
Make a letter uppercase by position number.
str_uppercase("hello world", 0) // Returns "Hello world".
Make a letter lowercase by position number.
str_lowercase("Hello World", 6) // Returns "Hello world".
Convert group of words into camel case.
str_to_camel("Hello World") // Returns "helloWorld".
Convert group of words into snake case.
str_to_snake("Hello World") // Returns "hello_world".
Convert group of words into kebab case.
str_to_kebab("Hello World") // Returns "hello-world".
Convert group of words into pascal case.
str_to_pascal("Hello World") // Returns "HelloWorld".
Convert string from camel case to snake case.
str_camel_to_snake("helloWorld") // Returns "hello_world".
Convert string from camel case to kebab case.
str_camel_to_kebab("helloWorld") // Returns "hello-world".
Convert string from camel case to pascal case.
str_camel_to_pascal("helloWorld") // Returns "HelloWorld".
Convert string from snake case to camel case.
str_snake_to_camel("hello_world") // Returns "helloWorld".
Convert string from snake case to kebab case.
str_snake_to_kebab("hello_world") // Returns "hello-world".
Convert string from snake case to pascal case.
str_snake_to_pascal("hello_world") // Returns "HelloWorld".
Convert string from kebab case to camel case.
str_kebab_to_camel("hello-world") // Returns "helloWorld".
Convert string from kebab case to snake case.
str_kebab_to_snake("hello-world") // Returns "hello_world".
Convert kebab case string to pascal case.
str_kebab_to_pascal("hello-world") // Returns "HelloWorld".
Convert string from pascal case string to camel case.
str_pascal_to_camel("HelloWorld") // Returns "helloWorld".
Convert string from pascal case to snake case.
str_pascal_to_snake("HelloWorld") // Returns "hello_world".
Convert string from pascal case to kebab case.
str_pascal_to_kebab("HelloWorld") // Returns "hello-world".
Convert camel case string to words.
str_camel_to_words("helloWorld") // Returns "hello world".
Convert snake case string to words.
str_snake_to_words("hello_world") // Returns "hello world".
Convert kebab case string to words.
str_kebab_to_words("hello-world") // Returns "hello world".
Convert pascal case string to words.
str_pascal_to_words("HelloWorld") // Returns "hello world".
Truncate string if exceeded the maximum charachters and automatically append "..." at the end of the string.
str_truncate("Hello World!!!", 8) // Returns "Hello...".
Generate a random string.
str_random(10, STR_RANDOM_DEFAULT); // Return random alphanumeric characters.
str_random(10, STR_RANDOM_NUMBERS); // Return random numbers.
str_random(10, STR_RANDOM_LETTERS); // Return random letters.
str_random(10, STR_RANDOM_LETTERS_UPPERCASE); // Return random uppercase letters.
str_random(10, STR_RANDOM_LETTERS_LOWERCASE); // Return random lowercase letters.
For issues, concerns and suggestions, you can email James Crisostomo via nerdlabenterprise@gmail.com.
This package is an open-sourced software licensed under MIT License.