- preg_replace: documentation ( source)
<?php
$filename = '10.jpg';
$size = 'profile';
echo preg_replace('/(\.[^.]*)$/', "-$size$1", $filename);
echo "\n";
$filename = 'john.smith.jpg';
$size = 'profile';
echo preg_replace('/(\.[^.]*)$/', "-$size$1", $filename);