Old 30-09-2005, 10:36   #1 (permalink)
gray
i still want paying
 
gray's Avatar
 
Join Date: Oct 2003
Location: newcastle, uk
Posts: 4,743
removing file extension with php

if i had filename.jpg and wanted it to be filename, what function do i use?
  Reply With Quote
Old 30-09-2005, 10:45   #2 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,202
Code:
$str = "filename.jpg"; echo substr($str, 0, 4);
?
  Reply With Quote
Old 30-09-2005, 10:47   #3 (permalink)
gray
i still want paying
 
gray's Avatar
 
Join Date: Oct 2003
Location: newcastle, uk
Posts: 4,743
hmm that changed it to "file"
  Reply With Quote
Old 30-09-2005, 10:50   #4 (permalink)
gray
i still want paying
 
gray's Avatar
 
Join Date: Oct 2003
Location: newcastle, uk
Posts: 4,743
i have to look for the . in the string and delete that and everything after it somehow
  Reply With Quote
Old 30-09-2005, 10:51   #5 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 5,090
you have to get the length of the string in chars first and work back from that. I will look up, wait there.
  Reply With Quote
Old 30-09-2005, 10:52   #6 (permalink)
gray
i still want paying
 
gray's Avatar
 
Join Date: Oct 2003
Location: newcastle, uk
Posts: 4,743
$var = "filename.jpg";
$newvar = preg_replace("#^(.*)\.[A-Za-z\.]+$#", "\\1", $var);
echo $newvar;

apparently
  Reply With Quote
Old 30-09-2005, 10:55   #7 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 5,090
$str = "filename.jpg";
$strlength = strlen($str);
$adjust = $strlength - 4;
echo substr($str, 0, $adjust);

something like that?
  Reply With Quote
Old 30-09-2005, 10:57   #8 (permalink)
dan
Iris Folder
 
dan's Avatar
 
Join Date: Apr 2003
Location: smokey
Posts: 2,649
or if it was always .jpg's you could use

$text = ereg_replace(".jpg", "" , $text);

but then that'd replace all instances of .jpg in the filename, so not so great. Your's is better.

(that'd be grays I was on about )
  Reply With Quote
Old 30-09-2005, 10:58   #9 (permalink)
datahound
Spare Parts
 
datahound's Avatar
 
Join Date: Jan 2005
Location: Bracknell Forest
Posts: 5,090
  Reply With Quote
Old 30-09-2005, 11:01   #10 (permalink)
gray
i still want paying
 
gray's Avatar
 
Join Date: Oct 2003
Location: newcastle, uk
Posts: 4,743
hah cheers lads, more than one way to skin a.. string.. sigh
  Reply With Quote
Old 30-09-2005, 11:03   #11 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,202
haha. or...

I missed the minus sign.

Code:
<?php $str = "filename.jpg"; echo substr($str, 0, -4); ?>
aslong as it's always a 3 character filename.

where's that cat.
  Reply With Quote
Old 08-10-2005, 09:35   #12 (permalink)
andymoo
Crazy Frog
 
Join Date: Jun 2004
Location: Liverpool
Posts: 42
Send a message via ICQ to andymoo Send a message via Yahoo to andymoo
thye other way would be to explode it through the dot that would create an array with two parts filename and jpg.

hth
  Reply With Quote
Old 05-11-2005, 11:16   #13 (permalink)
xml
Senior Member
 
Join Date: Sep 2004
Posts: 149
How about you make it usable for all extensions?

<?php
function cutExt($str) {
return substr($str, 0, strrpos($str, '.'));
}

echo cutExt('hello.ext') . '<br />' . cutExt('hello.sheep.sex') . '<br />' . cutExt('hello.gif') . '<br />' . cutExt('hello.jpg') . '<br />' . cutExt('hello.jpeg');
?>
__________________
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8