PHP str_ends_with: Check If a String Ends with a Substring (original) (raw)

Skip to content

Summary: in this tutorial, you’ll learn how to use the PHP str_ends_with() function to check if a string ends with a substring.

Introduction to the PHP str_ends_with() function #

The str_ends_with() function performs a case-sensitive search and checks if a string ends with a substring.

Here’s the syntax of the str_ends_with() function:

str_ends_with ( string <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>h</mi><mi>a</mi><mi>y</mi><mi>s</mi><mi>t</mi><mi>a</mi><mi>c</mi><mi>k</mi><mo separator="true">,</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi></mrow><annotation encoding="application/x-tex">haystack , string </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">ha</span><span class="mord mathnormal">ys</span><span class="mord mathnormal">t</span><span class="mord mathnormal">a</span><span class="mord mathnormal">c</span><span class="mord mathnormal" style="margin-right:0.03148em;">k</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span></span></span></span>needle ) : boolCode language: PHP (php)

The str_ends_with() function has two parameters:

The str_ends_with() function returns true if the $haystack ends with the $needle or false otherwise.

Note that every string ends with an empty string. Therefore, if the $needle is an empty (''), the str_ends_with() always returns true.

The str_ends_with() function has only been available since PHP 8.0.0. If you use PHP with a lower version, you can polyfill the function like this:

`<?php

if (!function_exists('str_ends_with')) { function str_ends_with($haystack, $needle) { return needle!==′′?substr(needle !== '' ? substr(needle!==′′?substr(haystack, -strlen($needle)) === $needle : true; } }`Code language: PHP (php)

PHP str_ends_with() function examples #

Let’s take some examples of using the PHP str_ends_with() function.

1) Using PHP str_ends_with() function with an empty substring example #

The following example returns true because every string ends with an empty string:

`<?php

echo str_ends_with('Hello there', '');`Code language: PHP (php)

Output:

1Code language: PHP (php)

2) Using PHP str_ends_with() function with single character example #

The following example uses the PHP str_ends_with() function to check if the string 'PHP tutorial' ends with the letter 'l':

`<?php

$str = 'PHP tutorial'; $substr = 'l'; result=strendswith(result = str_ends_with(result=strendswith(str, $substr) ? 'is' : 'is not';

echo "The strstr strresult ending with $substr";`Code language: PHP (php)

Output:

The PHP tutorial is ending with lCode language: PHP (php)

3) Using the PHP str_ends_with() function with multiple characters example #

The following example uses the PHP str_ends_with() function to check if the string 'PHP tutorial' ends with the string 'tutorial':

`<?php

$str = 'PHP tutorial'; $substr = 'tutorial'; result=strendswith(result = str_ends_with(result=strendswith(str, $substr) ? 'is' : 'is not';

echo "The strstr strresult ending with $substr";`Code language: PHP (php)

Output:

The PHP tutorial is ending with tutorialCode language: PHP (php)

4) Case-sensitive example #

It’s important to notice that the str_ends_with() function carries a case-sensitive search. For example:

`<?php

$str = 'PHP tutorial'; $substr = 'Tutorial'; result=strendswith(result = str_ends_with(result=strendswith(str, $substr) ? 'is' : 'is not';

echo "The strstr strresult starting with $substr";`Code language: PHP (php)

Output:

The PHP tutorial is not ending with TutorialCode language: PHP (php)

Summary #

Did you find this tutorial useful?