问题
I want to see if the last two characters/digits in a filename are numbers in PHP.
if (CODE HERE) {
// runs script because last two characters are numbers
}
This should set it off:
http://www.nws.noaa.gov/weather/images/fcicons/hi_shwrs20.jpg
The last two digits are '20'
This should not:
http://www.nws.noaa.gov/weather/images/fcicons/skc.jpg
There are no last two digits
回答1:
This should do the trick.
<?php
$filename = "http://www.nws.noaa.gov/weather/images/fcicons/skc23.jpg";
$posOfPeriod = strrpos($filename, ".");
$last2digits = substr($filename, $posOfPeriod -2, 2);
if (is_numeric($last2digits)) {
echo "Numeric: ".$last2digits;
}
else {
echo "Non-Numeric: ".$last2digits;
}
?>
来源:https://stackoverflow.com/questions/19738084/find-if-last-two-characters-in-filename-are-numbers