4

PHP DOMXPathクエリを使用して複数のタグ(tdとth)が必要です。

どうすればいいですか?

4

1 に答える 1

7

|(Union)演算子を使用できます。次に例を示します。

$doc = new DOMDocument();
$doc->loadHTML('<table>
<tr>
<th>table header</th>
<td>table cell</td>
</tr>
</table>');

$xpath = new DOMXPath($doc);

$rows = $xpath->query('//tr');                        // select all <tr> elements anywhere in the document
$cols = $xpath->query('./th | ./td', $rows->item(0)); // select all <th>/<td> from context
                                                      // where context = first row
echo $cols->length;             // 2
echo $cols->item(0)->nodeValue; // table header
echo $cols->item(1)->nodeValue; // table cell
于 2013-03-25T09:55:16.583 に答える