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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:41:17+00:00 2026-06-07T23:41:17+00:00

I am trying to capture all attributes of hyperlinks in HTML using PHP but

  • 0

I am trying to capture all attributes of hyperlinks in HTML using PHP but my regex is only returning the last attribute and value.

HTML:

$string = '
<a href="http://www.example.com/" style="font-weight: bold;">Example</a>
<a href="http://www.exampletwo.com/ style="font-weight: bold;">Example Two</a>
';

Regex:

preg_match_all('/<a(?: (.*?)="(.*?)")*>(.*?)<\/a>/i', $string, $result);

Result:

Array
(
    [0] => Array
        (
            [0] => <a href="http://www.example.com/" style="font-weight: bold;">Example</a>
            [1] => <a href="http://www.exampletwo.com/" style="font-weight: bold;">Example Two</a>
        )
    [1] => Array
        (
            [0] => style
            [1] => style
        )
    [2] => Array
        (
            [0] => font-weight: bold;
            [1] => font-weight: bold;
        )
    [3] => Array
        (
            [0] => Example
            [1] => Example Two
        )
)

How can I get it return all of the results from the repeating pattern?

  • 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-07T23:41:19+00:00Added an answer on June 7, 2026 at 11:41 pm

    If I may present an alternative to the oft-reviled ‘regex HTML parsing’:

    <?php
        $string = '
            <a href="http://www.example.com/" style="font-weight: bold;">Example</a>
            <a href="http://www.exampletwo.com/" style="font-weight: bold;">Example Two</a>
            ';
    
        $dom = new DOMDocument;
        $dom->loadHTML($string);
        $as = $dom->getElementsByTagName('a');
        foreach ($as as $a) {
            echo $a->nodeValue, '<br>';
            foreach ($a->attributes as $at) {
                echo $at->nodeName, ' ', $at->nodeValue, '<br>';
            }
            echo '<br><br>';
        }
    ?>
    

    use DOMDocument to parse your HTML and then simply tell it to give you all the anchor tags. If you suspect you’ll be dealing with massive HTML input, however, there’s always XMLReader, although you’ll have problems with non-proper or non-XHTML input with that.

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

Sidebar

Related Questions

I'm trying to capture certain parts of HTML using regular expressions, and I've come
Hey all, I'm using this colorpicker ( http://www.eyecon.ro/colorpicker ) and am trying to capture
Trying to use Excel VBA to capture all the file attributes from files on
I am trying to use regex to capture all the <div> and <span> tags
I'm trying to capture all the URLs requested to WAS. I'm running a web
I'm trying to create a created_at query range that will capture all records created
I trying to capture packets using SharpPcap library. I'm able to return the packets
I am trying to capture urls in an html page that is being repeated
I'm trying to figure out how to capture data from a form using EditText
I am trying to capture screenshots on test failure using selenium-client and rspec. I

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.