查找文件名中的最后两个字符是否为数字 [英] Find if last two characters in filename are numbers
本文介绍了查找文件名中的最后两个字符是否为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想查看文件名中的最后两个字符/数字是否是PHP中的数字.
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
}
这应该将其关闭:
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
推荐答案
这应该可以解决问题.
<?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;
}
?>
这篇关于查找文件名中的最后两个字符是否为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文