You could write a function to automatically add close tags to any opened tags that were not closed properly and make sure you don't ever cut out mid-tag.... but if you aren't a fairly good scriptor it may be difficult to accomplish. Certainly the small amount of script you have now wouldn't suffice.
Php has a relatively good xml parsing engine, so that would certainly help... you may even be able to find a script that does this by searching the web it can't be all that uncommon.