Returns true if the $haystack string starts with the $needle string.
string_starts_with($haystack, $needle)
Where:
- $haystack is the string to look into
- $needle is the string to check if the $haystack starts with
$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
Returns true if the $haystack string ends with the $needle string.
string_ends_with($haystack, $needle)
Where:
- $haystack is the string to look into
- $needle is the string to check if the $haystack starts with
$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
Returns the first occurrence of the string between the $start and $end string from $string. False if nothing is found.
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
$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:
Returns the strings between the $start and $end string from $string. Empty array if nothing is found.
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
$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] => )
True or false whether the string $needle is contained in the string $haystack or not.
string_contains($haystack, $needle)
Where:
- $haystack is the string to look into
- $needle is the string to check if the $haystack contains
$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