Maybe you should use addslashes() before you put your html in your database. Then you can get it back right with stripslashes(). Of course, if you already have badly formatted html in your database, this won't help. Instead, you could probably make a function that seeks the html strings for '=' and quotes the word/value following it. In this case you should see the manual for different string functions, like explode(), strreplace() etc.
from PHP manual:
string addslashes ( string str)
Returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote ('), double quote ("), backslash (\) and NUL (the NULL byte).
string stripslashes ( string str)
Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes (\\) are made into a single backslash (\).