PHP - 在最后一次出现针(不包括针)后获取子字符串


PHP - Get substring after last occurrence of needle not including needle

例如,如果我有abcxyz.doc并且想返回扩展名减去点,那么只需doc为子字符串。我不能依赖扩展名总是 3 个字符。

<?php
$x = 'abcxyz.doc';
ltrim(strrchr($x, '.'), '.');

这是我能做的最好的事情,还是有更优雅的方法可以做到这一点?

您可以使用已经为您执行此操作的$ext = pathinfo($filename, PATHINFO_EXTENSION):)