ztealmax Posted June 22, 2007 Share Posted June 22, 2007 Hi how would i remove the extencion .zip when echoing filenames? Using this code: <?php $dir = "thisisapath/"; $dh = opendir($dir); while (false !== ($filename = readdir($dh))) { $pattern = '/\.zip$/'; if(preg_match($pattern,$filename)) { /* You can proceed because the file has the extension .zip */ echo $filename . "<br>"; } } ?> //Cheers :-) Quote Link to comment https://forums.phpfreaks.com/topic/56729-remove-extencion-zip-from-filenames/ Share on other sites More sharing options...
Wildbug Posted June 22, 2007 Share Posted June 22, 2007 echo str_replace('.zip','',$filename); Quote Link to comment https://forums.phpfreaks.com/topic/56729-remove-extencion-zip-from-filenames/#findComment-280176 Share on other sites More sharing options...
metrostars Posted June 22, 2007 Share Posted June 22, 2007 you could do $filename = ereg_replace(".zip", "", "$filename"); and then echo that. EDIT: BEat me to it. Quote Link to comment https://forums.phpfreaks.com/topic/56729-remove-extencion-zip-from-filenames/#findComment-280177 Share on other sites More sharing options...
Wildbug Posted June 22, 2007 Share Posted June 22, 2007 echo substr($filename,0,-4); // the filename minus the last four chars (Don't use regular expressions for constant strings if you don't need anything special that regex would provide. It's faster to use str_replace.) Quote Link to comment https://forums.phpfreaks.com/topic/56729-remove-extencion-zip-from-filenames/#findComment-280178 Share on other sites More sharing options...
ztealmax Posted June 22, 2007 Author Share Posted June 22, 2007 Thanx alot works like a charm, thank you for the quick responces... //Cheers Quote Link to comment https://forums.phpfreaks.com/topic/56729-remove-extencion-zip-from-filenames/#findComment-280184 Share on other sites More sharing options...
ztealmax Posted June 22, 2007 Author Share Posted June 22, 2007 Now for an even more stupid question if it echos out dir/filename (.zip extencion now removed using this code) $filename = ereg_replace(".zip", "", "$filename"); Now if i would like to also remove the search path dir/ thats infront of the filename, how would i do that? //Cheers Quote Link to comment https://forums.phpfreaks.com/topic/56729-remove-extencion-zip-from-filenames/#findComment-280193 Share on other sites More sharing options...
Wildbug Posted June 22, 2007 Share Posted June 22, 2007 echo preg_replace('|.*/(.*)\.zip$|i','$1',$filename); Quote Link to comment https://forums.phpfreaks.com/topic/56729-remove-extencion-zip-from-filenames/#findComment-280194 Share on other sites More sharing options...
ztealmax Posted June 22, 2007 Author Share Posted June 22, 2007 Yea how do i remove the searchpath infront of the filename? Quote Link to comment https://forums.phpfreaks.com/topic/56729-remove-extencion-zip-from-filenames/#findComment-280207 Share on other sites More sharing options...
Wildbug Posted June 22, 2007 Share Posted June 22, 2007 See previous post. Quote Link to comment https://forums.phpfreaks.com/topic/56729-remove-extencion-zip-from-filenames/#findComment-280247 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.