it was mainly foam so maybe it was over carbonated and releasing pressure created foam, and it was this foam tha twas pushed out by pressure. only had about 16 litres in a 19 lite keg so plenty of room, and it was standing straight.That's not the context that we're talking about though. You said that beer came out of the gas post and into your regulator. Unless the level of the beer in the keg was too high, that can't happen, because gravity.
Edit - if you were force carbing and the keg was on its side, and you were rocking it about, you might have collected a bunch of foam in the gas dip tube. When you turned the gas off on the cylinder, the pressure in the keg shoots that foam up the gas line and into your reg.
Maybe disconnect the quick connect from the keg, then switch off the gas at the cylinder. Then no matter what happens, you can't get beer up the gas line.
good to know about disconnecting the quick connect - can you do that during a force carb before turning the reg down to 0 psi? ie when its at 20 psi for example?