It is necessary to insert text at the end of the name of the file being downloaded (before the extension), for example. (site.ru). The extension is always just mp3.

So

track (site.ru) .mp3

Piece of code

class download { var $properties = array ('old_name' => "", 'new_name' => "", 'type' => "", 'size' => "", 'resume' => "", 'max_speed' => "" ); var $range = 0; function download($path, $name = "", $resume = 0, $max_speed = 0) { $name = ($name == "") ? substr( strrchr( "/" . $path, "/" ), 1 ) : $name; $name = explode( "/", $name ); $name = end( $name ); $type = explode( ".", $name ); $type = strtolower( end( $type ) ); 

Here

$ path is the name on the server

$ name is the name that is written to the file and which needs to be changed. It is now track.mp3 , but you need so track (site.ru) . Mp3 .

Tried to add after $ name = end ($ name); to just remove .mp3

 $name = preg_replace("/.*?\./", '', $name); 

but it remains the other way around.

  • preg_replace("/(.*)\.mp3$/", "$1(site.ru).mp3", $fname) - teran

3 answers 3

The easiest way to do this is through the str_replace function:

 $name = end( $name ); $name = str_replace('.mp3', '(site.ru).mp3', $name); 

More reliable option:

 $name = substr_replace($name, '(site.ru).mp3', -4); 

The option is not the best, but the easiest.

  • Yeah: str_replace('.mp3', '(site.ru).mp3', 'test.mp3.mp3'); // "test(site.ru).mp3(site.ru).mp3" str_replace('.mp3', '(site.ru).mp3', 'test.mp3.mp3'); // "test(site.ru).mp3(site.ru).mp3" . - user207618
  • I said that the option is not the best, and there will be no track called test.mp3 - Yaroslav Molchan
  • Everything works, and if there is no such name as test.mp3, otherwise everything will be fine? - steep
  • Yes, it is still possible to make it more reliably through substr_replace, the answer added - Yaroslav Molchan

Without special witchcraft:

 var_dump(preg_replace('/(\.mp3)$/', "(site.ru)$1", 'track.mp3')); // string(18) "track(site.ru).mp3" 

http://regexr.com/3fgh1

    If without regular expressions, you can use the function strstr ()

     function filename($file, $url='(site.ru)') { $expansion = strstr($file, '.'); $fileName = strstr($file, '.', true); return $fileName . $url . $expansion; } echo filename('track.mp3');//track(site.ru).mp3 

    But in my opinion, the preg_replace () suggested above is better.