-3

ファイル名の最後の 2 文字/数字が PHP の数字かどうかを確認したい。

if (CODE HERE) {
 // runs script because last two characters are numbers
}

これはそれを引き立たせるはずです:

http://www.nws.noaa.gov/weather/images/fcicons/hi_shwrs20.jpg
The last two digits are '20'

これはすべきではありません:

http://www.nws.noaa.gov/weather/images/fcicons/skc.jpg
There are no last two digits
4

1 に答える 1

3

これでうまくいくはずです。

<?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;
    }
?>
于 2013-11-02T01:50:10.673 に答える