Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8049377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:46:11+00:00 2026-06-05T06:46:11+00:00

I am not good at regex. I got this string with some html <a>

  • 0

I am not good at regex.

I got this string with some html <a> tags and i need to strip some of them out, but only <a> tags containing a certain value in the “href” attribut (mysite.com).

I still need to keep the link text, like the php function strip_tags($string, "<a>")

Here is what I got so far.:

/<a href=["\']?(http:\/\/mysite.com[^"\'\s>]+)["\'\s>]?/i

This is also working, but the problem is that I can’t get the “text” in fx. <a href="http://mysite.com/testing/something">"text"</a>

So the input.:

Lorem ipsum <a href="http://mysite.com/testing/something">dolor</a> sit amet, <a href="http://keepingThisLink.com">consectetur</a> adipiscing elit. Duis dignissim <a href="http://mysite.com/testing">golor</a> vitae turpis fermentum tincidunt.

Should output.:

Lorem ipsum dolor sit amet, <a href="http://keepingThisLink.com">consectetur</a> adipiscing elit. Duis dignissim golor vitae turpis fermentum tincidunt.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:46:14+00:00Added an answer on June 5, 2026 at 6:46 am

    Here’s a more reliable, DOM-based approach:

    <?php
    
    $a = 'Lorem ipsum <a href="http://mysite.com/testing/something">dolor</a> sit amet, <a href="http://keepingThisLink.com">consectetur</a> adipiscing elit. Duis dignissim <a href="http://mysite.com/testing">golor</a> vitae turpis fermentum tincidunt.';
    
    $domd = new DOMDocument();
    libxml_use_internal_errors(true);
    $domd->loadHTML($a);
    
    $domx = new DOMXPath($domd);
    foreach ($domx->query("//a") as $link) {
        $href = $link->getAttribute("href");
        if ($href === "http://keepingThisLink.com") {
            continue;
        }
    
        $text = $domd->createTextNode($link->nodeValue);
    
        $link->parentNode->replaceChild($text, $link);
    }
    
    //unfortunately saveHTML adds doctype and a few unneccessary tags    
    var_dump(preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $domd->saveHTML())));
    

    the output is:

    string(161) "
    <p>Lorem ipsum dolor sit amet, <a href="http://keepingThisLink.com">consectetur</a> adipiscing elit. Duis dignissim golor vitae turpis fermentum tincidunt.</p>
    "
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not so good with regex, and need some help. I have a string
I am not so good with regex, I need some help and I am
I am not very good at regex, but I need to convert the following
I am not very good in regex but there is an illogic thing that
I'm not very good on regex, I need to search for < anyword />
I'm not very good at regex but maybe there's a simple way to achieve
Im not good at math, I could find everything so far, but not this
I've got some HTML that looks like this: <tr class=row-even> <td align=center>abcde</td> <td align=center><a
I am not very good in regex so I'm looking for help. I need
I am not very good with Regex but I am learning. I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.