You can use a regex with a positive representation. Like this:
$withdots = 'abc.def.ghij-klmnop.q234.mp3';
$nodots = preg_replace('/\.(?=.*\.)/', '', $withdots);
After doing the above, $nodotswill contain abcdefghij-klmnopq234.mp3. A regular expression basically indicates the coincidence of all periods followed by another period. Therefore, the last period will not match. We replace all matches with an empty string, and we leave the desired result.
source
share