Q:

Write a PHP program to test if a given string occurs at the end of another given string

0

Write a PHP program to test if a given string occurs at the end of another given string.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
function str2_in_str1($str1, $str2) {
  $p_len = strlen($str2);
   $w_len = strlen($str1);
   $w_start = $w_len-$p_len-1;
   if (substr($str1, $w_len-$p_len, $p_len) == $str2) {
      return "true";
   } 
   else 
   {
      return "false";
   }
}
echo str2_in_str1("Python","on")."\n";
echo str2_in_str1("JavaScript","php")."\n";
?>

Sample Output:

true                                                        
false

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now