You can try the following:
preg_replace('#<([^ ]+)((\s+[\w]+=((["\'])[^\5]+\5|[^ ]+))+)>#e', '"<\\1" . str_replace("\\\'", "\'", strip_tags("\\2")) . ">"', $code);
It takes every html opening tag ( <something> ), matches all the attributes name="value" name='value' name=value , then it tags them. str_replace necessary because when the e modifier is added, PHP uses addslashes for each match before evaluating it.
I tested it and it works fine. :)
source share