Jump to content

change file extension


denoteone

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.