Find if last two characters in filename are number

2019-06-14 20:04发布

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

标签: php filenames
1条回答
萌系小妹纸
2楼-- · 2019-06-14 20:33

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;
    }
?>
查看更多
登录 后发表回答