'Use PHP DOMParser to change malformed php tags

I have a plugin that is exporting some malformed php tags & I don't have any control over the initial export: Is it possible to change the php tags using PHP DOMParser before the php is run on the server?

Malformed Ending php bracket code:

<blocs>
                    }
                </blocs>

Code the way it should look when it runs:

<?php } ?>

I tried this, but I'm still stumped:

foreach ($dom->getElementsByTagName("blocs") as $node) {
  $blocsnode = $dom->createElement("?php", $node->nodeValue);
  $dom->replaceChild($blocsnode, $node);
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source