简体   繁体   English

如何用PHP替换短语中的单词?

[英]How to replace words from a phrase in PHP?

I have couple of mp3 files named 01 - Saanson Ko (Zid) - 128Kbps.mp3 and 01 - Saanson Ko (Zid) - 320Kbps.mp3 我有几个名为01 - Saanson Ko (Zid) - 128Kbps.mp3的mp3文件和01 - Saanson Ko (Zid) - 320Kbps.mp3

I want it to be renamed to 01 - Saanson Ko (Zid) - Muzic.Asia I tried the following code but it did not work. 我希望将其重命名为01 - Saanson Ko (Zid) - Muzic.Asia我尝试了以下代码,但没有用。 How to do it? 怎么做?

$file = "01 - Saanson Ko (Zid) - 128Kbps.mp3";
$file = "01 - Saanson Ko (Zid) - 320Kbps.mp3";
$mp3_filename = str_replace(".mp3","",$file);
$mp3_filename = str_replace("128Kbps","Muzic.Asia",$file);
$mp3_filename = str_replace("320Kbps","Muzic.Asia",$file);
echo "$file";

str_replace() returns the results. str_replace()返回结果。

Try these codes : 尝试以下代码:

$file = "01 - Saanson Ko (Zid) - 128Kbps.mp3";
$file = "01 - Saanson Ko (Zid) - 320Kbps.mp3";
$file = str_replace(".mp3","",$file);
$file = str_replace("128Kbps","Muzic.Asia",$file);
$file = str_replace("320Kbps","Muzic.Asia",$file);
echo "$file";

This should work for you: 这应该为您工作:

You can use preg_replace() and capture the parts which you want and replace it with Muzic.Asia , eg 您可以使用preg_replace()并捕获所需的零件,并用Muzic.Asia替换它,例如

$file = preg_replace("/ - \d+\w+\.\w+$/", " - Muzic.Asia", $file);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM