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 6137291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:42:27+00:00 2026-05-23T17:42:27+00:00

i have a html document with n a href tags with different target urls

  • 0

i have a html document with n “a href” tags with different target urls and different text between the tag.

For example:

<a href="http://www.example.com/d?12345abc" name="example"><span ....>lorem ipsum</span></a>
<a href="http://www.example.com/d/d?abc1234" name="example2"><span ....>example</span></a>
<a href="http://www.example.com/d.1234" name="example3">example3</a>
<a href="http://www.example.com/d/d.1234" name="example4"><img ...>test</img></a>
<a href="http://www.example.com/without_d/1234" name="example3">without a d as target url</a>

As you can see the target urls switch between “d?, d., d/d?, d/d.” and between the “a tag” there could be any type of html which is allowed by w3c.

I need a Regex which gives me all links which has one of these combination in the target url:
“d?, d., d/d?, d/d.” and has “Lorem” or “test” between the “a tags” in any position including sub html tags.

My Regex so far:

href=[\"\']([^>]*?/[d]+[.|\?][^"]*?[\"\'][^>]*[/]?>.*?</a>)

I tried to include the lorem / test as followed:

href=[\"\']([^>]*?/[d]+[.|\?][^"]*?[\"\'][^>]*[/]?>(lorem|test)+</a>)

but this will only works if I put a “.*?” before and after the (lorem|test) and this would be to greedy.

If there is a easier way with SimpleXml or any other DOM parser, please let me know. Otherwise I would appreciate any help with the regex.

Thanks!

  • 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-05-23T17:42:27+00:00Added an answer on May 23, 2026 at 5:42 pm

    Here you go:

    $html = array
    (
        '<a href="http://www.example.com/d?12345abc" name="example"><span ....>lorem ipsum</span></a>',
        '<a href="http://www.example.com/d/d?abc1234" name="example2"><span ....>example</span></a>',
        '<a href="http://www.example.com/d.1234" name="example3">example3</a>',
        '<a href="http://www.example.com/d/d.1234" name="example4"><img ...>test</img></a>',
        '<a href="http://www.example.com/without_d/1234" name="example3">without a d as target url</a>',
    );
    
    $html = implode("\n", $html);
    $result = array();
    $anchors = phXML($html, '//a[contains(., "lorem") or contains(., "test")]');
    
    foreach ($anchors as $anchor)
    {
        if (preg_match('~d[.?]~', strval($anchor['href'])) > 0)
        {
            $result[] = strval($anchor['href']);
        }
    }
    
    echo '<pre>';
    print_r($result);
    echo '</pre>';
    

    Output:

    Array
    (
        [0] => http://www.example.com/d?12345abc
        [1] => http://www.example.com/d/d.1234
    )
    

    The phXML() function is based on my DOMDocument / SimpleXML wrapper, and goes as follows:

    function phXML($xml, $xpath = null)
    {
        if (extension_loaded('libxml') === true)
        {
            libxml_use_internal_errors(true);
    
            if ((extension_loaded('dom') === true) && (extension_loaded('SimpleXML') === true))
            {
                if (is_string($xml) === true)
                {
                    $dom = new DOMDocument();
    
                    if (@$dom->loadHTML($xml) === true)
                    {
                        return phXML(@simplexml_import_dom($dom), $xpath);
                    }
                }
    
                else if ((is_object($xml) === true) && (strcmp('SimpleXMLElement', get_class($xml)) === 0))
                {
                    if (isset($xpath) === true)
                    {
                        $xml = $xml->xpath($xpath);
                    }
    
                    return $xml;
                }
            }
        }
    
        return false;
    }
    

    I’m too lazy not to use this function right now, but I’m sure you can get rid of it if you need to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML document where I would like to semantically group text to
I have a string that represents an html document. I'm trying to replace text
Change this... www.sample.com/sample.html#commentxxx?stuffhereIdontneed into this... www.sample.com/sample.html I have it in a link so I
Given that in a html document all anchor elements have a href attribute that
I currently have this in my document $(document).ready(function(){ $([href$='.html']).addClass('html'); $([href$='.pdf']).addClass('pdf'); }); which styles any
i have simple html that looks like this : <div class=img> <a target=_blank href=kl_big.htm
I have an HTML document that is using to embed a control. In some
I have an HTML document as a string I want to search for a
I have a HTML document with the below setup: <div class=main-div style=padding: 5px; border:
I have a html document and I want to delete all the divs of

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.