Remove everything after last .
Moderator: General Moderators
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
Remove everything after last .
I need a regex that will remove everything after the last . found in a string. Basically, I am removing the file extension from a file that is being uploaded.
Can anyone help?
Can anyone help?
I wonder if this would be faster than a regex (untested):
Code: Select all
$string = 'filename.something.txt';
$apart = explode('.', $string);
$ext = array_pop($apart);
$filename = implode('.', $apart);if you decide to go with regex, something like this should work:
Code: Select all
$pattern = "/\.[a-z0-9]+$/i";- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Code: Select all
echo substr($string, 0, strrpos(".", $string));oooo...I like that one, but make sure you reverse the order of the arguments in strrposd11wtq wrote:Code: Select all
echo substr($string, 0, strrpos(".", $string));
Code: Select all
echo substr($string, 0, strrpos($string,"."));- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Code: Select all
$str = "filename.php";
$str = preg_replace("/(\\.).*/","\\1",$str); // filename.- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Let's see, was that search for a 'needle' in a 'haystack' or search the 'haystack' for a 'needle'? Hmmm...Burrito wrote:oooo...I like that one, but make sure you reverse the order of the arguments in strrposd11wtq wrote:Code: Select all
echo substr($string, 0, strrpos(".", $string));![]()
Code: Select all
echo substr($string, 0, strrpos($string,"."));