How to cut extension from the file name?

The question was asked: 6 years 11 months ago   views: 19

How to cut extension from the file name? for example, the file is called restore.php so we need to cut the "php" and withdraw.

Asked: oldzas oldzas 21-06-2012 в 12:31:47
We advise you to use all of the solution c pathinfo(). The regular season is not the same. - ArchDemon ArchDemon 26-08-2018 в 10:50:41

Answers   3

$file = '/var/www/restore.php';
echo pathinfo($file, PATHINFO_FILENAME);
Answered: Rjazhenka Rjazhenka 23-04-2015 в 07:52:35
echo preg_replace('/\.\w+$/', ", 'restore.php'); // restore

UPD: the Second option:

echo basename('/www/htdocs/inc/'); //
Answered: kite kite 21-06-2012 в 12:36:45
@KiTE, the file /tmp/conf.d/tra-ta-ta that will cut ? - avp avp 21-06-2012 в 12:41:51
From /tmp/conf.d/tra-ta-ta the regular season /\.\w+$/ won't cut. \w is same as [a-zA-Z0-9_] - kite kite 21-06-2012 в 12:46:17
I understand you cut off before the expansion and vivezli, and I need the file name and extension, you couldn't tweak the code ? - oldzas oldzas 21-06-2012 в 13:01:31
Also because of the problem with a normal (for *nix) names, because any character except '/' and '.' can be in the extension. Ie the regular season dB. some other. It should be mapped to the rightmost point and all but the '/' after this point. - avp avp 21-06-2012 в 13:07:42
@avp, Yes, you are right. Then, as an option, /\.[^\.\/]+$/ - kite kite 21-06-2012 в 13:13:26
$str = 'example.docx';
echo substr($str,strripos($str,'.')+1); // docx
Answered: user280431 user280431 26-08-2018 в 09:37:27
Discover the magical world pathinfo -- and immediately eliminate the need to create their own bikes. - A K A K 26-08-2018 в 11:42:54
What happens if the name of the two dots? - Эникейщик Эникейщик 26-08-2018 в 11:57:23
Strripos - nahodit the last occurrence of a character - user280431 user280431 27-08-2018 в 12:11:54