Ваш текущий подход близок, но чтобы заставить его работать, мы можем попробовать использовать обходные пути.Замените:
(?<=^|\.)(?=\.|$)
на 0
.
$mystring = "2.3.5...9";
$output = preg_replace('/(?<=^|\.)(?=\.|$)/', '0', $mystring);
echo $output;
Вот краткое объяснение логики регулярных выражений:
(?<=^|\.) position is preceded by either the start of the string, or another dot
(?=\.|$) position is followed by either the end of the string, or another dot