假设我在 PHP 中有这个函数
<?php
function get_filename_parts($fname){
/*
receives a filename and returns an array with the filename & extension
example:
receives: myfile1.jpg
returns: array('filename' => 'myfile1', 'extension' => 'jpg')
receives: myfile1.blah.blah.jpg
returns: array('filename' => 'myfile1.blah.blah', 'extension' => 'jpg')
*/
$name_no_ext = explode('.',$fname);
$ext = array_pop($name_no_ext);
$name_no_ext = implode('.',$name_no_ext);
$retval = array('filename' => $name_no_ext, 'extension' => $ext);
return $retval;
}
?>
是否有一些语法可以在单个语句中执行类似的操作:
<?php echo get_filename_parts('test.txt')->filename; ?>
代替:
<?php $fname = get_filename_parts('test.txt'); echo $fname['filename']; ?>
谢谢!