- rtrim: documentation ( source)
- preg_replace: documentation ( source)
<?php
$filename = '..hel...lo....wor.......ld.......';
echo 'Original: ', $filename, "\n";
$filename = preg_replace( '/\.{2,}/', '.', $filename );
echo 'After preg_replace(): ', $filename, "\n";
$filename = rtrim( $filename, '.' );
echo 'After rtrim(): ', $filename, "\n";
$filename .= '.png';
echo 'Final: ', $filename;