In this post we will be creating a PHP script that will check if an existing string ends with a certain character or word. To do this we will be using the ‘str_ends_with()’ function which checks if a string ends with a given substring.
Our string for the example will be ‘hello world’, see the snippet of code below where we check if our string ends with the letter ‘d’.
<?php
$string = 'hello world';
if (str_ends_with($string, 'd')) {
echo "String ends with 'd'\n";
}
?>
The above would return ‘String ends with ‘d’‘ as output.
See the snippet of code below where we check if our string ends with the word ‘world’.
<?php
$string = 'hello world';
if (str_ends_with($string, 'world')) {
echo "String ends with 'world'\n";
}
?>
The above would return ‘String ends with ‘world’‘ as output.