hiding file extension it self
I looking for your help please
I use simple php code like:
$file = $_SERVER["SCRIPT_NAME"];
//echo $file;
$break = Explode('/', $file);
$pfile = $break[开发者_StackOverflow中文版count($break) - 1];
echo $pfile;
than output $pfile e.g. fileforme.php
but that i want is output of $pfile become fileforme
because i want use it to:
$txt['fl']= $pfile ;
how to do? or there are any another better way?
You can use pathinfo() and its PATHINFO_FILENAME flag (only available for php 5.2+) e.g.
$file = $_SERVER["SCRIPT_NAME"];
echo 'file: ', $file, "\n";
echo 'PATHINFO_FILENAME: ', pathinfo($file, PATHINFO_FILENAME);
prints (on my machine)
file: C:\Dokumente und Einstellungen\Volker\Desktop\test.php
PATHINFO_FILENAME: test
See basename in the PHP Manual:
$path = "/home/httpd/html/index.php";
$file = basename($path); // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"
or use pathinfo if you do not know the extension:
$path_parts = pathinfo('/www/htdocs/index.html');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
Basically you're looking for the filename without the extension:
$filename = basename($_SERVER["SCRIPT_NAME"], ".php");
Note that this answer is specific for the php extension.
$pfile_info = pathinfo($pfile);
$ext = $pfile_info['extension'];
See pathinfo()
for further information.
精彩评论