PHP 替代 Javascript 的 pop() 方法

Moh*_*qui -3 javascript php split

有什么PHP方法可以做:

var filename = $_POST["filename"];
filename = filename.split(".").pop();
Run Code Online (Sandbox Code Playgroud)

我怎样才能在 PHP 中做同样的事情?

Ama*_*ali 5

像这样的东西,也许?

$filetype = array_pop(explode('.',$filename));
Run Code Online (Sandbox Code Playgroud)

它会生成一个Only variables should be passed by reference通知。如果你想摆脱它,你需要:

$fileparts = explode('.',$filename); 
$filetype = array_pop($fileparts);
Run Code Online (Sandbox Code Playgroud)

但是,获取文件扩展名的最佳方法是使用pathinfo()

$filetype = pathinfo($filename, PATHINFO_EXTENSION);
Run Code Online (Sandbox Code Playgroud)