tags:

views:

128

answers:

4

I am trying to pull the filename out of a directory without the extension.

I am kludging my way through with the following:

foreach ($allowed_files as $filename) { 
  $link_filename = substr(basename($filename), 4, strrpos(basename($filename), '.'));
  $src_filename = substr($link_filename, 0, strrpos($link_filename) - 4);
  echo $src_filename;
}

...But that can't work if the extension string length is more than 3. I looked around in the PHP docs to no avail.

+2  A: 

list($file) = explode('.', $filename);

dmitrig01
what about `blah.class.php` ?
nickf
hi dmitrig01- what is list($file) in this case? any variable?
jml
I would do it like this and remove the last element after the explode.
wenbert
i don't know how to remove the last element. can you post an example of how to use this syntax?
jml
My code only preserves the first element. list($file) says "take the first element and put it in variable $file".If you want all but the last element, use substr($filename, 0, strrpos($filename, '.'));
dmitrig01
OK; great. thanks for the info.
jml
+1  A: 

Try this:

$noExt = preg_replace("/\\.[^.]*$/", "", $filename);

Edit in response to cletus's comment:
You could change it in one of a few ways:

$noExt = preg_replace("/\\.[^.]*$/", "", basename($filename));

// or

$noExt = preg_replace("/\\.[^.\\\\\\/]*$/", "", $filename);

Yes, PHP needs regex literals...

nickf
i take it back- totally works if you use basename($filename)
jml
I found this to be the most useful, since you are operating on the string itself. It seems like it can be very useful to take the regex approach when you know precisely what string it is that you have to modify.
jml
-1 This is a good example of why you shouldn't reinvent the wheel of something PHP does with an inbuilt function. What about the string "/some/path.to/afile"?
cletus
good point. thanks cletus.
jml
+3  A: 

PHP has a handy pathinfo() function that does the legwork for you here:

foreach ($allowed_files as $filename) {
  echo pathinfo($filename, PATHINFO_FILENAME);
}

Example:

$files = array(
  'somefile.txt',
  'anotherfile.pdf',
  '/with/path/hello.properties',
);

foreach ($files as $file) {
  $name = pathinfo($file, PATHINFO_FILENAME);
  echo "$file => $name\n";
}

Output:

somefile.txt => somefile
anotherfile.pdf => anotherfile
/with/path/hello.properties => hello
cletus
what is the difference between pathinfo($filename, PATHINFO_BASENAME) and basename($filename) ?
jml
Typo on my part. PATHINFO_BASENAME is same as basename(). It's PATHINFO_FILENAME you want (updated).
cletus
This is great. Thanks.
jml
I think you also have a typo where you echo out- Should be $file => $name . "\n"; right? Or can you use string representation of internal variables as such with PHP?
jml
Hm; actually this doesn't seem to work for me. I get nothing returned when I do the operation on either a string akin to the files array you posted *or* a parsed string of my choice including the file extention (i.e. "happy.jpg").
jml
@jml: No, the above code is correct. See http://php.net/manual/en/language.types.string.php, especially http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double for PHP string syntax.
cletus
+2  A: 

try this

function file_extension($filename){
    $x = explode('.', $filename);
    $ext=end($x);
    $filenameSansExt=str_replace('.'.$ext,"",$filename);
    return array(
        "filename"=>$filenameSansExt,
        "extension"=>'.'.$ext,
        "extension_undotted"=>$ext
        );
}

usage:

$filenames=array("file1.php","file2.inc.php","file3..qwe.e-rt.jpg");
foreach($filenames as $filename){
    print_r(file_extension($filename));
    echo "\n------\n";

}

output

Array
(
    [filename] => file1
    [extension] => .php
    [extension_undotted] => php
)

------
Array
(
    [filename] => file2.inc
    [extension] => .php
    [extension_undotted] => php
)

------
Array
(
    [filename] => file3..qwe.e-rt
    [extension] => .jpg
    [extension_undotted] => jpg
)

------
pǝlɐɥʞ
Thanks for this. Good to know how to solve the problem with a custom function.
jml
This for giving a complete and clear example.
wenbert