-2

I want to remove links (keeping text) from a string only if the text inside the link match exactly a term.

I'm not sure what's wrong with this regexp, but it strips out a lot of text.

$terms = ["justo pulvinar"];
$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed <a href=\"#/app/292\">quam nibh, faucibus</a> quam nibh, faucibus eget tortor eget, finibus semper justo. aliquet <a href=\"#/app/212\">justo pulvinar</a>, elit.";
foreach ($terms as $term) {
    $string = preg_replace('/<a href=\"(.*?)\">('.$term.')<\/a>/', "\\2",$string);
 }
echo $string;

Edit maybe I wasn't clear enough, I have a $terms array for a reason: I want to remove from $string links that match exactly those terms, not all links.

alfredopacino
  • 2,979
  • 9
  • 42
  • 68
  • If you want to keep the text and remove the link, why not just use `strip_tags()`? – ild flue Jan 25 '18 at 19:03
  • Careful using regex to parse HTML for [H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – ctwheels Jan 25 '18 at 19:08
  • `(.*?)` is greedy, it takes everything from `\"` before `/app/292` to `\">` after `app/212` . that's why u lost lot of text. – ild flue Jan 25 '18 at 19:09

3 Answers3

2

Everything looks okay except your replacement operation. This one works for me:

$string = preg_replace('/<a [^>]*>'.$term.'<\/a>/', $term, $string)

Here, we are matching an <a and all characters after it which are not > because the attributes of the a tag are not relevant for the operation. Then we want to see the term we're working on, and a closing a tag.

Replace all of that with just the term. I'm not using backreferences because there should be no variance in the string values. A scenario where backreferences might be useful is if case were variable and needed to be preserved.

sorak
  • 2,607
  • 2
  • 16
  • 24
1

I have try some regExr and found something I think it will work for you

    $terms = ["justo pulvinar"];
    $string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed <a href=\"#/app/292\">quam nibh, faucibus</a> quam nibh, faucibus eget tortor eget, finibus semper justo. aliquet <a href=\"#/app/212\">justo pulvinar</a>, elit.";
    $string = preg_replace('/<a\s+(?:[^>]*?\s+)?href=([\'"])(.*?)\1>/', "", $string);
    echo $string;
Puneet Sharma
  • 305
  • 3
  • 14
  • 1
    Don't tell them you found something, tell them what you did and why you did it that way. A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Jan 25 '18 at 19:29
  • Okay I will keep in mind for future .. Thank you @Jayblanchard – Puneet Sharma Jan 26 '18 at 08:52
0
$terms = ["justo pulvinar"];
$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed <a href=\"#/app/292\">quam nibh, faucibus</a> quam nibh, faucibus eget tortor eget, finibus semper justo. aliquet <a href=\"#/app/212\">justo pulvinar</a>, elit.";
foreach ($terms as $term) {
$string = strip_tags($term);
echo $string;
}
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Jan 25 '18 at 19:29