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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:31:54+00:00 2026-06-04T05:31:54+00:00

Lately I’ve had a question, what I’m trying to do is read data from

  • 0

Lately I’ve had a question, what I’m trying to do is read data from an HTML table and grab the data into a variable called $id. For example I have this code:

<tr>
<td>413</td>
<td>Party Hat</td>
<td>0</td>
<td>No</td>
<td><a href="http://clubpenguincheatsnow.com/tools/swfviewer/items.swf?id=413">View SWF</a></td>
</tr>

What I want to do is that another variable called $array[$i] which is holding a search query. I want my PHP code to search through the table until it finds the section with that specific query in it. In this case is would be “Party Hat.” What I want it to do after it finds the query is for it to look at the ID which is the “td” section above the name “Party Hat” the ID in this case is 413. After this I want the variable $id to hold the ID. How do I do this? Any help would be HIGHLY appreciated!

  • 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-04T05:31:55+00:00Added an answer on June 4, 2026 at 5:31 am

    using Tidy, DOMDocument and DOMXPath (make sure the PHP extensions are enabled) you can do something like this:

    <?php
    $url = "http://example.org/test.html";
    
    function get_data_from_table($id, $url)
    {
        // retrieve the content of that url
        $content = file_get_contents($url);
    
        // repair bad HTML
        $tidy = tidy_parse_string($content);
        $tidy->cleanRepair();
        $content = (string)$tidy;
    
        // load into DOM
        $dom = new DOMDocument();
        $dom->loadHTML($content);
    
        // make xpath-able
        $xpath = new DOMXPath($dom);
    
        // search for the first td of each tr, where its content is $id
        $query = "//tr/td[position()=1 and normalize-space(text())='$id']";
        $elements = $xpath->query($query);
        if ($elements->length != 1) {
            // not exactly 1 result as expected? return number of hits
            return $elements->length;
        }
    
        // our td was found
        $element = $elements->item(0);
    
        // get his parent element (tr)
        $tr = $element->parentNode;
        $data = array();
    
        // iterate over it's td elements
        foreach ($tr->getElementsByTagName("td") as $td) {
            // retrieve the content as text
            $data[] = $td->textContent;
        }
    
        // return the array of <td> contents
        return $data;
    }
    
    echo '<pre>';
    print_r(
        get_data_from_table(
            414,
            $url
        )
    );
    echo '</pre>';
    

    Your HTML source (http://example.org/test.html):

    <table><tr>
    <td>413</td>
    <td>Party Hat</td>
    <td>0</td>
    <td>No</td>
    <td><a href="http://clubpenguincheatsnow.com/tools/swfviewer/items.swf?id=413">View SWF</a></td>
    </tr><tr>
    <td>414</td>
    <td>Party Hat</td>
    <td>0</td>
    <td>No</td>
    <td><a href="http://clubpenguincheatsnow.com/tools/swfviewer/items.swf?id=413">View SWF</a></td>
    </tr>
    

    (as you can see, no valid HTML, but this doesn’t matter)

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

Sidebar

Related Questions

Lately I am involved in processing data from sensors from different devices. These sensors
Lately I had to change some code on older systems where not all of
Lately I've be moving source files around in our source tree. For example placing
Lately I have been trying my hands on Eclipse IDE for java development. I
Lately I have been playing around with intents and bundles. I thought I had
Lately, I've been reading much about constructors from the well-received C++ FAQ . One
Lately something weird has been happening to my projects in xcode: I've been trying
Lately I had an issue with an array that contained some hundred thousands of
Lately I've been running into some subtle layout issues in my iOS app. For
Lately i've had issues with ADT14/15 and their eclipse integration for debugging/building/cleaning android apps.

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.