php通过关联替换文件扩展名

时间:2020-12-19 08:18:44

I've setup a preg_replace to add a .gz file extension to image filenames. But I need to change the file extension associatively (so that bmp -> bmp.gz, png -> png.gz, etc.). What can I put in place of the $old_extension variable to do this?

我已经设置了一个preg_replace来为图像文件名添加.gz文件扩展名。但我需要以关联方式更改文件扩展名(以便bmp - > bmp.gz,png - > png.gz等)。我可以用什么代替$ old_extension变量来执行此操作?

$new_file_ext = 
preg_replace('"\.(bmp|gif|png|jpg|jpeg)$"', '.' . $old_extension . '.gz', $filename );

3 个解决方案

#1


1  

You need to back-refrence the chars present inside the group index 1. Capturing groups have the nature of capturing al the characters which are matched by the pattern present inside those groups. So here the pattern present inside the capturing group is bmp|gif|png|jpg|jpeg. In the replacement part, we could refer those chars which are present in a group by specifying it's index number like \1 (which refers to the first capturing group).

您需要反向引用组索引1中存在的字符。捕获组具有捕获与这些组内存在的模式匹配的字符的性质。所以这里捕获组内存在的模式是bmp | gif | png | jpg | jpeg。在替换部分中,我们可以通过指定它的索引号来引用组中存在的那些字符,例如\ 1(指的是第一个捕获组)。

preg_replace('~\.(bmp|gif|png|jpg|jpeg)$~', '.\1.gz', $filename );

DEMO

DEMO

#2


1  

Why don't you simply append the extension to it?

你为什么不简单地将扩展名附加到它?

$new = $old.".gz";

#3


0  

Better approach is strtr function, it takes an assoc array, like this:

更好的方法是strtr函数,它需要一个assoc数组,如下所示:

<?php    
$trans = array("bpm" => "bpm.gz","jpg" => "jpg.gz");
echo strtr($filename, $trans);
?>

edit: to be sure extension is replaced, do the usual

编辑:确保更换扩展名,按常规操作

  1. $ext = explode('.',$filename) explode
  2. $ ext = explode('。',$ filename)爆炸
  3. $ext[key(end($ext))] = strtr($ext[key(end($ext))], $trans); translate just extension
  4. $ ext [key(end($ ext))] = strtr($ ext [key(end($ ext))],$ trans);翻译只是扩展
  5. implode back return join('',$ext);
  6. implode back return join('',$ ext);

#1


1  

You need to back-refrence the chars present inside the group index 1. Capturing groups have the nature of capturing al the characters which are matched by the pattern present inside those groups. So here the pattern present inside the capturing group is bmp|gif|png|jpg|jpeg. In the replacement part, we could refer those chars which are present in a group by specifying it's index number like \1 (which refers to the first capturing group).

您需要反向引用组索引1中存在的字符。捕获组具有捕获与这些组内存在的模式匹配的字符的性质。所以这里捕获组内存在的模式是bmp | gif | png | jpg | jpeg。在替换部分中,我们可以通过指定它的索引号来引用组中存在的那些字符,例如\ 1(指的是第一个捕获组)。

preg_replace('~\.(bmp|gif|png|jpg|jpeg)$~', '.\1.gz', $filename );

DEMO

DEMO

#2


1  

Why don't you simply append the extension to it?

你为什么不简单地将扩展名附加到它?

$new = $old.".gz";

#3


0  

Better approach is strtr function, it takes an assoc array, like this:

更好的方法是strtr函数,它需要一个assoc数组,如下所示:

<?php    
$trans = array("bpm" => "bpm.gz","jpg" => "jpg.gz");
echo strtr($filename, $trans);
?>

edit: to be sure extension is replaced, do the usual

编辑:确保更换扩展名,按常规操作

  1. $ext = explode('.',$filename) explode
  2. $ ext = explode('。',$ filename)爆炸
  3. $ext[key(end($ext))] = strtr($ext[key(end($ext))], $trans); translate just extension
  4. $ ext [key(end($ ext))] = strtr($ ext [key(end($ ext))],$ trans);翻译只是扩展
  5. implode back return join('',$ext);
  6. implode back return join('',$ ext);