I found a solution which uses DOMDocument but does not add extra tags to your strings; just fixes malformed HTML. See answer here: https://stackoverflow.com/a/79081559/492132
Original github (not mine) here: https://gist.github.com/hubgit/1322324