I’m about to do a search/replace in a MySql database. I want to replace one word in a field containing html text with another word. The problem is that the word that I’m looking for in some cases is part of a path. Eg.:
<… src=”../SEARCHTERM/img.jpg” />
Obviously I do not want to replace this instance. So my question is: What is the best way to do this? How do I replace only if the word is not part of a path?
The problematic part of this is item 2. Depending on your processing needs, a regular expression replacement will do it (like preg_replace in PHP) or you need a fully fledged HTML parser.
MySQL can match strings using regular expressions, but no built-in way exists to do replacement based on regular expressions. You can, however, use User Defined Functions to do that, e. g. MySQL Regular Expression UDFs. Then again, question is whether regular expression are sufficient for your replacement needs. In a lot of cases involving HTML, it is not.