0

I have an old MySQL database and I need to remove different HTML links from description field.

So, for example after SQL execution on description with a following text:

Hello <a href="http://www.example1.com">test</a> World !

I need to get:

Hello World !

Please note that in my database HTML links are not the same and contain different addresses and texts.

Is it possible to do with MySQL SQL query and if so, could you please provide an example.

4
  • Is each column guaranteed to have only one anchor tag? Commented Nov 27, 2016 at 10:34
  • Right now I only seen one anchor tag in the text.. but I suppose can be columns with 2+ anchor tags..
    – alexanoid
    Commented Nov 27, 2016 at 10:38
  • Here's the deal: In general parsing nested HTML can't even be done with regex, you need a parser. But if the structure is known and fixed, you could handle it with regex, or even with a clever application of MySQL's string functions. So if you can give us a structure, maybe you will get an answer. Commented Nov 27, 2016 at 10:40
  • By the way, the reason I'm asking this now is to prevent someone from answering only to find out that there is an edge case you never told us about. Commented Nov 27, 2016 at 10:42

1 Answer 1

2

You can use a query like this. You only must change FIELDNAME to your fieldname and TABLENAME to your tablename. In your Sample there are one space behind HELLO ** and one before ** WORLD, so you have 2 spaces in the RESULT

SELECT
CONCAT(
    SUBSTR(FIELDNAME,1,
    INSTR(FIELDNAME,'<a href=')-1)
,
    SUBSTR(FIELDNAME,
    INSTR(FIELDNAME,'</a>')+4)
)
FROM YOURTABLE;

sample

SELECT 
CONCAT(
    SUBSTR('Hello <a href="http://www.example1.com">test</a> World !',1,
    INSTR('Hello <a href="http://www.example1.com">test</a> World !','<a href=')-1)
,
    SUBSTR('Hello <a href="http://www.example1.com">test</a> World !',
    INSTR('Hello <a href="http://www.example1.com">test</a> World !','</a>')+4)
);

result

Hello  World !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.