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
Share on other sites

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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.