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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:24:35+00:00 2026-06-04T17:24:35+00:00

I have the following script which loops through a HTML table and gets the

  • 0

I have the following script which loops through a HTML table and gets the values from it then returns the value of the table in a td.

$tds = $dom->getElementsByTagName('td');

                // New dom
                $dom2 = new DOMDocument;

                $x = 1;
                // Loop through all the tds printing the value with a new class
                foreach($tds as $t) {
                    if($x%2 == 1)
                        print "</tr><tr>";

                    $class = ($x%2 == 1) ? "odd" : "even";

                    var_dump($t->textContent);

                    print "<td class='$class'>".$t->textContent."</td>";
                    $x++;
                }

But the textContent seems to be stripping the HTML tags (for example it is a <p></p> wrapper tag). How can I get it to just give me the value?

Or is there another way of doing this? I have the following html

<table>
<tr>
<td>q1</td>
<td>a1</td>
</tr>
<tr>
<td>q2</td>
<td>a2</td>
</tr>
</table>

and I need to make it look like

<table>
<tr>
<td class="odd">q1</td>
<td class="even">a1</td>
</tr>
<tr>
<td class="odd">q2</td>
<td class="even">a2</td>
</tr>
</table>

It will always look the exact same way (minus extra element rows and the values which change).

Any help?

  • 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-04T17:24:38+00:00Added an answer on June 4, 2026 at 5:24 pm

    According to MDN this is the expected behaviour of textContent.

    You can just add the class to the tds in the DomDocument

    $tds = $dom->getElementsByTagName('td');
    $x = 1;
    foreach($tds as $td) {
        if($x%2 == 1){
            $td->setAttribute('class', 'odd');
        }
        else{
            $td->setAttribute('class', 'even');
        }
        $x++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following script which I am running. It loops through each table
I have the following script currently, which gets the category IDs as arrays: $sql_select_categories
I have the following script which does not work 100%, it returns about 20
I have a FileMaker script which calculates a value. I have 1 record from
I have the following script which I want to debug: function getTabFrame() { $.ajax({
I have the following script which first shows only the first para of the
I have the following script which appears multiple times on my page. I have
I have the following script which will delete a file off a BlackBerry. However,
I have the following script, which when you click the image within the div,
I have the following script, which is working for the most part Link to

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.