Find if last two characters in filename are numbers [closed]

青春壹個敷衍的年華 提交于 2019-12-08 14:20:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!