Check if a string ends with given word in PHP


Create a function to check whether a string ends with the specified string or not. The function should return TRUE on success or FALSE on failure.

The following is the syntax −

endFunc(str, lastStr)

Consider the following parameters in it to check −

  • str − The string to be tested

  • lastStr − The text to be searched in the end of the specified string.

Example

The following is an example −

 Live Demo

<?php
   function endFunc($str, $lastString) {
      $count = strlen($lastString);
      if ($count = = 0) {
         return true;
      }
      return (substr($str, -$count) = = = $lastString);
   }
   if(endFunc("pqrstuv","rst"))
      echo "True!";
   else
      echo "False!";
?>

Output

The following is the output −

False!

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Dec-2019

383 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements