If anyone else has this problem add these two lines into the LoadHTML function
$s = str_replace("<?php","<?php",$s);
$s = str_replace("?>","?>",$s);
This will render the php code as a string, then make sure you replace them again in your write code and it all works. It's a total hack but seems to work fine.