This seems weird idea but maybe it's what you are looking for if you want to do it in PHP without touching HTML/JS:
The above is preety OK in theory, but in practice it will result in VERY memory consuming app. So be warned.
And please don't downvote me, this is only a theoretical dispute.