I'm trying to implement the function below. Would it be best to use some type of regex here? I need to capture the number too.
Kirk OuimetHow 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. Determining if string ends with another string duplicate Ask Question 2. PHP regex: each word must end with dot. Php find a substring only if it's not part of another substring. Java String endsWith Method: The endsWith method is used to check if a specified string ends with the specified suffix.
Kirk Ouimet9,0373434 gold badges9595 silver badges148148 bronze badges
3 Answers
the regex is explained as follows:
.*? - this will take up all the characters in the string from the start up until a match for the subsequent part is also found.
(d+)$ - this is one or more digits up until the end of the string, grouped.
without the ? in the first part, only the last digit will be matched in the second part because all digits before it would be taken up by the .*
Kinjal DixitKinjal Dixit6,05422 gold badges4343 silver badges6363 bronze badges
return is_numeric(substr($string, -1, 1));
This only checks to see if the last character in the string is numerical, if you want to catch and return multidigit numbers, you might have to use a regex.
![Php if string ends with Php if string ends with](/uploads/1/2/5/6/125616065/613454179.jpg)
An appropriate regex would be
Reese MooreReese Moore/[0-9]+$/
which will grab a numerical string if it is at the end of a line.10.2k33 gold badges1818 silver badges2929 bronze badges
in my opinion The simple way to find a string ends with number is
shihab mmshihab mm