views:

47

answers:

4

Hi all, i looking for your help please

i use simple php code like:

$file = $_SERVER["SCRIPT_NAME"];
//echo $file;
$break = Explode('/', $file);
$pfile = $break[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? Regards,

+3  A: 

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
Gordon
ahh... you give me nice tips, thank you..
jones
+3  A: 

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
VolkerK
Yeah... thank you for your kindly, GBU
jones
+2  A: 

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.

cballou
thank you bro, i Think this great thing..
jones
+1  A: 
$pfile_info = pathinfo($pfile);
$ext = $pfile_info['extension'];

See pathinfo() for further information.

Psaniko
also you... thank you man...
jones