Reputation: 145
How should I replace dots with underlines without losing the file extension?
$str = $_FILES['files']['name']; //file.name.word.jpg
$ext = end(explode('.', $str));
$filename = explode('.', $str);
//output file_name_word.jpg
ps: it needs to be before upload.. if the user uploads a file with dots it must to be renamed and inserted on db
Upvotes: 0
Views: 1971
Reputation: 17227
$str = "file.name.word.jpg";
$regex = "/(\.)(?=\S+\.)/";
echo preg_replace($regex, "_", $str);
short form
echo preg_replace("/(\.)(?=\S+\.)/", "_", "file.name.word.jpg");
Upvotes: 2
Reputation: 76636
Use pathinfo()
to extract the file name and str_replace()
to remove all the dots out of it.
$filename = pathinfo('/path/to/your/file');
echo str_replace('.', '_', $filename['filename']);
Upvotes: 4