I have a string that contains text and some links. I want to remove all the links that do not have absolute links but keep their text.
I want to do it with regex instead of loading the dom.
Is that possible?
I have tried this regex
preg_replace('#href=["']([^/][^':"]*)["']#', $root_path.'$1', $html);
But this will replace the href with an empty string only.
And I want to completely remove the anchor tags and keep its text only.
2
Answers
You can use this regex, but take it with a pinch of salt! It’s far from perfect, so try it for your use case and if anything goes wrong, just write in the comment and I’ll try to fix the regex for you!
You can simplify your regex
Output will be like that
Run in sandbox:
https://onlinephp.io/c/3bda8