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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:00:45+00:00 2026-05-27T07:00:45+00:00

I need to extract some data from a webpage with php. The part that

  • 0

I need to extract some data from a webpage with php. The part that I’m interested in is structured similarly to this:

<a href="somepath" target="fruit">apple</a>
<a href="somepath" target="animal">cat</a>
<a href="somepath" target="fruit">orange</a>
<a href="somepath" target="animal">dog</a>
<a href="somepath" target="fruit">mango</a>
<a href="somepath" target="animal">monkey</a>

First, I want to extract all fruits, and then all animals, so that I have them nicely grouped.

I figured out how to loop through all attribute values. Here’s the code:

$dom = new DOMDocument();
$html = file_get_contents('example.html');

@$dom->loadHTML($html);

$a = $dom->getElementsByTagName('a');

for ($i; $i < $a->length; $i++) {
$attr = $a->item($i)->getAttribute('target');

echo $attr . "\n";
}

So I get:

fruit animal fruit animal fruit animal

I also found out how to get the elements’ text content:

$a->item($i)->textContent

So, if included in loop and echoed, I get:

apple cat orange dog mango monkey

I feel like I’m very close, but I can’t get what I want. I need something like this:

if ( target = “fruit”) then give me “apple, orange, mango”.

Can someone please point me in the right direction?

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-27T07:00:45+00:00Added an answer on May 27, 2026 at 7:00 am

    Just continue on target attributes which aren’t fruit, and then add the textContent of the elements to an array.

    $nodes = array();
    
    for ($i; $i < $a->length; $i++) {
        $attr = $a->item($i)->getAttribute('target');
    
        if ($attr != 'fruit') {
            continue;
        }
    
        $nodes[] = $a->item($i)->textContent;
    }
    

    $nodes now contains all the nodes of the elements which have their target attribute set to fruit.

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

Sidebar

Related Questions

I have this situation where I need to get some data from a webpage
Sometimes I need to quickly extract some arbitrary data from XML files to put
I need to extract financial price data from a binary file. This price data
I need to extract some data from a CSV file. The CSV is a
I need to extract some management information (MI) from data which is updated in
I need to extract data from a DB2 table, run some processing on each
I've got and sql express database I need to extract some data from. I
I have a chunk of data from which I need to extract some strings
I've just discovered (by necessity) that TSQL has some functionality to extract data from
I'm trying to extract some textual data from a PDF file. To do this,

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.