Reputation: 406
I'm trying to extract data from HTML code in a PHP variable.
The HTML is the following:
<tr class="tr1">
<td align="right">
1.
</td>
<td align="left">
<input type="hidden" name="now[8116632]" value="98" />
<input type="hidden" name="add[8116632]" value="39" />
<input type="hidden" name="sec_value[8116632]" value="45720000" />
<div id="uid8116632"></div>
</td>
<td align="left">
<a href="playerInfo.phtml?pid=8116632" target="_blank"
onclick="return(openSmallWindow('playerInfo.phtml?pid=8116632',
c41c569a1b9c46c4cbc4bc58f37cb05f'))">kana</a>
</td>
<td align="right">
98
</td>
<td align="right">
45.720.000
</td>
</tr>
<tr class="tr2">
<td align="right">
2.
</td>
<td align="left">
<input type="hidden" name="now[8121292]" value="90" />
<input type="hidden" name="add[8121292]" value="45" />
<input type="hidden" name="sec_value[8121292]" value="36500000" />
<div id="uid8121292"></div>
</td>
<td align="left">
<a href="playerInfo.phtml?pid=8121292" target="_blank"
onclick="return(openSmallWindow('playerInfo.phtml?pid=8121292',
'c41c569a1b9c46c4cbc4bc58f37cb05f'))">Xoán Manuel Pérez Chaves</a>
</td>
<td align="right">
90
</td>
<td align="right">
36.500.000
</td>
</tr>
In this code I want to extract:
<td align="right">1.</td>
This number as $position
<input type="hidden" name="now[8116632]" value="98" />
The idnumber which is between square bracks in attributte name as $id, the value as $value
<input type="hidden" name="sec_value[8121292]" value="36500000" />
This value as $price
My php code so far is this:
$DOM = new DOMDocument;
$DOM->loadHTML($result2);
$xpath = new DomXpath($DOM);
$div = $xpath->query('//*[@class="tr1"]');
Now how can I get the variables I tell you guys before? $position,$id,$value and $price
Thank you in advance
Upvotes: 0
Views: 420
Reputation: 3577
For element text use something like
.../td[@align='right']/text()
For attribute values use something like
.../td[@align='left']/input[1]/@value
and for parsing the brackets, you're going to have to use the substring functions, which is going to get messy. substring-before and substring-after are probably what you're looking for. Check this out as a reference: xpath: string manipulation
Upvotes: 2
Reputation: 20162
I would rather use more input with hidden type and give them appropriate names like:
<tr class="tr1">
<td align="right">1.</td>
<input type="hidden" name="position" value="1" />
<input type="hidden" name="id" value="765434" />
<input type="hidden" name="value" value="89898989" />
<input type="hidden" name="price" value="100" />
<td>Some description</td>
</tr>
It will be much easier to maintain such code and you will have no problem with accessing variables in php. I'm sorry if this is now you are asking for but for me it's reasonable solution (I may not know all the facts).
If you are not able to change this HTML I recommend you to check SimpleHTMLDom library instead of xpath here are some info:
http://simplehtmldom.sourceforge.net/manual.htm
Upvotes: 1