Have you ever needed to search your database for specific text, but HTML tags got in the way? Here’s a nifty MySQL query that solves this problem:
SELECT * FROM posts
WHERE REGEXP_REPLACE(content, "<[^>]*>", "") LIKE '%I am "Asking" a ques%'
This query uses REGEXP_REPLACE to strip out HTML tags before performing the search. Let’s break it down:
REGEXP_REPLACE(content, "<[^>]*>", "")
removes all HTML tags from the ‘content’ column.- The regular expression
"<[^>]*>"
matches any HTML tag. - We replace these matches with an empty string, effectively removing them.
- The
LIKE
operator then searches the cleaned text for our phrase.
This approach allows you to search your content as if it were plain text, ignoring any HTML formatting. It’s particularly useful for blogs or content management systems where posts are stored with HTML markup.