0

I have an HTML Table and its each rows are identified with an unique id. I'm trying to get the label text inside one of row. I tried with $dom->getElementById($id); but this provides all text values coming under that element.

Here is the structure:

<table>
<tr id="1">.....</tr>
<tr id="2">
<td>
<span>Some text</span>
</td>
<td>
.
.  //Some html elements
.
<table>
<tbody>
<tr>
<td>
<label>label1</label>
</td>                         //I want to collect these labels
<td>
<label>label2</label>
</td>
</tr>
</tbody>
</tabel>
.
.
.

I tried with this $elements = $dom->getElementById('2'); code. It provides all values together.

Output

 ["nodeValue"]=> string(37) "Select your options *option 1option 2" 

How can I get only label text.

1 Answer 1

2

Have you tried with getElementsByTagName? E.g. :

$elements = $dom->getElementById('2')->getElementsByTagName('label');

(edited):

then you'll be able to access the items:

for($i=0;$i<$elements->length;$i++) {
       echo "Item $i: " . $elements->item($i)->textContent . "\n";
}
Sign up to request clarification or add additional context in comments.

3 Comments

It returns only length. object(DOMNodeList)#416 (1) { ["length"]=> int(2) }
then, it seems you should be able to access item(x). I'll edit the answer.
Perfect. I didn't thought it is possible to access the elements in this way. thanks for help...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.