thomas-squall / string-utils
Utilities for string management
This package is auto-updated.
Last update: 2024-10-20 04:10:22 UTC
README
List of available functions
string_starts_with
Description
Returns true if the $haystack string starts with the $needle string.
Definition
string_starts_with($haystack, $needle)
Where:
- $haystack is the string to look into
- $needle is the string to check if the $haystack starts with
Usage
$string = "Hello World"; echo "Starts with Hello? " . string_starts_with($string, "Hello"); echo PHP_EOL; echo "Starts with World? " . string_starts_with($string, "World");
This will print:
Starts with Hello? true Starts with World? false
string_ends_with
Description
Returns true if the $haystack string ends with the $needle string.
Definition
string_ends_with($haystack, $needle)
Where:
- $haystack is the string to look into
- $needle is the string to check if the $haystack starts with
Usage
$string = "Hello World"; echo "Ends with Hello? " . string_ends_with($string, "Hello"); echo PHP_EOL; echo "Ends with World? " . string_ends_with($string, "World");
This will print:
Ends with Hello? false Ends with World? true
string_between
Description
Returns the first occurrence of the string between the $start and $end string from $string. False if nothing is found.
Definition
string_between($string, $start, $end, $empty_string = false)
Where:
- $string is the string to look into
- $start is the left string to search
- $end is the right string to search
- $empty_string determines whether return empty strings or not
Usage
$string_1 = "|Hello|World|"; $string_2 = "||"; echo "String 1: " . string_between($string_1, "|", "|"); echo PHP_EOL; echo "String 2 with $empty_string false: " . string_between($string_2, "|", "|"); echo PHP_EOL; echo "String 2 with $empty_string true: " . string_between($string_2, "|", "|", true);
This will print:
String 1: Hello
String 2: false
String 2:
strings_between
Description
Returns the strings between the $start and $end string from $string. Empty array if nothing is found.
Definition
strings_between($string, $start, $end, $empty_strings = false)
Where:
- $string is the string to look into
- $start is the left string to search
- $end is the right string to search
- $empty_strings determines whether return empty strings or not
Usage
$string = "|Hello|World||"; echo "With $empty_strings false:" . PHP_EOL; print_r(strings_between($string, "|", "|")); echo PHP_EOL; echo "With $empty_strings true:" . PHP_EOL; print_r(strings_between($string, "|", "|", true));
This will print:
With $empty_strings false: Array ( [0] => Hello [1] => World ) With $empty_strings true: Array ( [0] => Hello [1] => World [2] => )
string_contains
Description
True or false whether the string $needle is contained in the string $haystack or not.
Definition
string_contains($haystack, $needle)
Where:
- $haystack is the string to look into
- $needle is the string to check if the $haystack contains
Usage
$string = "Hello"; echo "Contains Hello? " . string_contains($string, "Hello"); echo PHP_EOL; echo "Contains World? " . string_contains($string, "World");
This will print:
Contains Hello? true Contains World? false