Remove last two digits of filename [closed]

2019-06-14 18:34发布

How can I remove the last two characters of a filename in PHP?

http://www.nws.noaa.gov/weather/images/fcicons/tsra50.jpg
# becomes
http://www.nws.noaa.gov/weather/images/fcicons/tsra.jpg

and

http://www.nws.noaa.gov/weather/images/fcicons/hi_shwrs60.jpg
# becomes
http://www.nws.noaa.gov/weather/images/fcicons/hi_shwrs.jpg

The extension will always be 3 characters (4 with the period).

1条回答
Emotional °昔
2楼-- · 2019-06-14 19:15

You could use a negative value as start to substr_replace if you know that the number always is two digits:

$filename = 'filename11.jpg';
$newfilename = substr_replace($data, '', -6, 2);
查看更多
登录 后发表回答