I have a large database which contains records that have <a>
tags in them and I would like to remove them. Of course there is the method where I create a PHP script that selects all, uses strip_tags
and updates the database, but this takes a long time. So how can I do this with a simple (or complicated) MySQL query?
相关问题
- sqlyog export query result as csv
- NOT DISTINCT query in mySQL
- MySQL: conduct a basic search
- Why sometimes there is one of more gap(s) in the v
- mySQL alter table on update, current timestamp
I am passing this code on, seems very similar to the above. Worked for me, hope it helps.
REPLACE()
works pretty well.The subtle approach:
...and the not so subtle: (Converting strings into slugs)
I just extended the answer @boann to allow targetting of any specific tag so that we can replace out the tags one by one with each function call. You just need pass the tag parameter, e.g.
'a'
to replace out all opening/closing anchor tags. This answers the question asked by OP, unlike the accepted answer, which strips out ALL tags.I don't believe there's any efficient way to do this in MySQL alone.
MySQL does have a
REPLACE()
function, but it can only replace constant strings, not patterns. You could possibly write a MySQL stored function to search for and replace tags, but at that point you're probably better off writing a PHP script to do the job. It might not be quite as fast, but it will probably be faster to write.Here you go:
I made sure it removes mismatched opening brackets because they're dangerous, though it ignores any unpaired closing brackets because they're harmless.
Boann's works once I added
SET $str = COALESCE($str, '');
.from this post: