2

How do one determine,if the strings appears at the end of the other string. If they do then print true to standard out and false if they don’t. Would strpos will help?

    Sample Input

S1: “staff”
S2: “FF”

how would i make a function to run this,

  function matching_ends($s1,$s2){

}
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Ryan Diaz
  • 43
  • 1
  • 6
  • One option would be using a decent language that has a [`.endswith()`](http://docs.python.org/2/library/stdtypes.html#str.endswith) method on strings. ;) – ThiefMaster Jan 20 '14 at 07:56
  • PHP 8.0 introduces new methods for this job str_starts_with and str_end_with: http://stackoverflow.com/a/64160081/7082164 – Jsowa Nov 30 '20 at 16:49

2 Answers2

5
<?php
$s1="testing";
$s2="ing";

echo matching_ends($s1, $s2);

function matching_ends($s1,$s2){
    return substr($s1, -strlen($s2)) == $s2 ? "true" : "false";
}
?>

Demo

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
1

You can use this

if( substr($s1, strlen($s1)-1) === substr($s2, strlen($s2)-1))
{
     // Do something when character at the last matches
}
else{
     // Do something when doesn't match
}