我想显示当前页面的文件名作为页面标题,但不带扩展名。如果可能的话,第一个字符应该大写。这个是可能的吗?
发布于 2012-08-31 00:06:39
每个人都喜欢一行程序:
ucfirst(pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME))
pathinfo()
的第二个参数从文件名中剥离路径和扩展名(PHP >= 5.2)
顺便说一句,我使用的是$_SERVER['PHP_SELF']
而不是__FILE__
,因为如果代码从另一个文件中运行,它就会崩溃;-)
发布于 2012-08-30 23:59:32
如果你想从script.php
转到Script
,可以:
// pathinfo give info about given file and returns it into an associative array:
$info = pathinfo( __FILE__ );
// the filename key holds file name without extension nor parent path:
$title = ucwords( $info['filename'] );
发布于 2012-08-31 00:01:18
对于需要大写的第一个字母,请使用ucfirst()
。这就是大写第一个字符函数。你也可以对大写的单词使用ucwords()
。
要获得不带扩展名的文件名,请使用pathinfo()
funciton。
$path_parts = pathinfo(__FILE__);
echo ucfirst($path_parts['filename']);
https://stackoverflow.com/questions/12200877
复制相似问题