Let's say I have a file named Icon.png
and I'm going to zip it, it will get Icon.png.zip
but I want it to just call Icon.zip
, how do I remove the .png
part of the file name?
Let's say I have a file named Icon.png
and I'm going to zip it, it will get Icon.png.zip
but I want it to just call Icon.zip
, how do I remove the .png
part of the file name?
If you use PHP 5.2.0 upwards, you can use the pathinfo
function with the PATHINFO_FILENAME
constant, this will return only the file name. So, just concatenate .zip
:
<?php
$nome_arquivo = "Icon.png";
$novo_nome = pathinfo($nome_arquivo, PATHINFO_FILENAME) . '.zip';
echo $novo_nome; // Retorna "Icon.zip"
?>
Ideone: link
For versions below, you need to use the PATHINFO_BASENAME
constant, however this will return the whole file name (using the first code as an example, Icon.png
). What you can do is use preg_replace
to get the filename and then concatenate .zip
. Example:
<?php
$nome_arquivo = "Icon.png";
$arquivo = pathinfo($nome_arquivo, PATHINFO_BASENAME);
$novo_nome = preg_replace('/(.+?)\.[^.]*$|$/', '$1', $nome_arquivo) . '.zip';
echo $novo_nome; // Retorna "Icon.zip"
?>
Ideone: link
You can use the str_replace function, see a working example here .
<?php
$valor = "Icon.png.zip";
$valor = str_replace(".png", "", $valor);
echo $valor;
?>
You can also pass an array containing the words you want to replace:
<?php
// array com os valores que devem ser substuidos
$ext = array(".png", ".jpg", ".gif");
$png = "IconPNG.png.zip";
$png = str_replace($ext, "", $png);
$jpg = str_replace($ext, "", "IconJPG.jpg.zip");
echo $png . "\n";
echo $jpg;
?>
Functional example here .