2016-02-29 197 views
0

那麼假設我有字符串:file.mp3。我如何根據文件的擴展名動態修改它,如file_0.mp3。那麼_0總是在擴展之前?將字符串添加到另一個字符串中

+1

什麼字符串什麼樣的?你可以使用正則表達式,甚至可以使用'str_replace'。在你現在的例子中'$ string = str_replace('。','_0。',$ string);'我認爲會這樣做。 – chris85

+0

你是什麼意思由'什麼是字符串'? – Alex

+0

'file.mp3'是'.'唯一的'.',並且總是位於文件擴展名之前?你有沒有擴展名的文件? – chris85

回答

2

如果要動態地做到這一點,你不能確定如果字符串可能包含不止一個,在這裏「」:

<?php 

$string = 'file.mp3'; 

$ext = strrpos($string, '.'); // find the last "dot" 
$newname = substr($string, 0, $ext) . '_0' . substr($string, $ext); 

echo $newname; 
相關問題