denoteone Posted May 3, 2010 Share Posted May 3, 2010 I have a file that has any file extension. I want to change that to a .flv below is what I have so far. $newfile = myvideo.avi; //I need to find and replace what ever is after the "." with flv I know it is some thing like: $file_name_array = explode($newfile,".") $file_name_array[1] = "flv"; $final_name = file_name_array[0] . file_name_array[1] echo $final_name; The output will be myvideo.flv Link to comment https://forums.phpfreaks.com/topic/200561-change-file-extension/ Share on other sites More sharing options...
siric Posted May 3, 2010 Share Posted May 3, 2010 You have the explode the wrong way around should be $file_name_array = explode(".", $newfile); and $final_name = $file_name_array[0].".".$file_name_array[1]; //was missing $ and correct concatenation Don't forget your semicolons at the ends of the lines Link to comment https://forums.phpfreaks.com/topic/200561-change-file-extension/#findComment-1052449 Share on other sites More sharing options...
Muddy_Funster Posted May 3, 2010 Share Posted May 3, 2010 I am probably WAY off mark here, but wouldnt this also work? $file_name = $file_name_array['0']; $new_name = str_replace($substr($file_name, -3, 3), 'flv', $file_name); Link to comment https://forums.phpfreaks.com/topic/200561-change-file-extension/#findComment-1052454 Share on other sites More sharing options...
siric Posted May 3, 2010 Share Posted May 3, 2010 I am probably WAY off mark here, but wouldnt this also work? $file_name = $file_name_array['0']; $new_name = str_replace($substr($file_name, -3, 3), 'flv', $file_name); You do not even have to place the name in an array to do it this way. Simply $file_name = 'myvideo.avi'; $new_name = str_replace(substr($file_name, -3, 3), 'flv', $file_name); That would work. The limitation here is that it will only work with file extensions of 3 characters in length. And remember, no $ in front of the substr function name. Link to comment https://forums.phpfreaks.com/topic/200561-change-file-extension/#findComment-1052713 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.