@ 2013-11-03T21:50:04Z <?php
$html = <<<EOF
<table>
<tr>
<td>something</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td></td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td></td>
<td>3</td>
<td>3</td>
</tr>
<tr>
<td>anything</td>
<td>4</td>
<td>4</td>
</tr>
<tr>
<td></td>
<td>5</td>
<td>5</td>
</tr>
<tr>
<td> </td>
<td>6</td>
<td>6</td>
</tr>
<tr>
<td>nothing</td>
<td>7</td>
<td>7</td>
</tr>
<tr>
<td></td>
<td>8</td>
<td>8</td>
</tr>
<tr>
<td>ontehing</td>
<td>9</td>
<td>9</td>
</tr>
<tr>
<td></td>
<td>10</td>
<td>10</td>
</tr>
</table>
EOF;
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$query = '
//tr[td[1][contains(text(), "anything")]]
|
//tr[td[1][contains(text(), "anything")]]
/following-sibling::tr[td[1][not(text())] and preceding-sibling::tr[1][td[1][not(text()) or contains(text(), "anything")]]]
';
foreach ($xpath->query($query) as $res) {
var_dump($doc->saveHTML($res));
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.0.30 , 8.1.22 - 8.1.28 , 8.2.9 - 8.2.18 , 8.3.0 - 8.3.6 string(78) "<tr>
<td>anything</td>
<td>4</td>
<td>4</td>
</tr>"
string(70) "<tr>
<td></td>
<td>5</td>
<td>5</td>
</tr>"
Output for 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.29 , 8.1.0 - 8.1.21 , 8.2.0 - 8.2.8 string(69) "<tr><td>anything</td>
<td>4</td>
<td>4</td>
</tr>"
string(61) "<tr><td></td>
<td>5</td>
<td>5</td>
</tr>"
Output for 5.3.6 - 5.3.29 , 5.4.0 - 5.4.45 , 5.5.0 - 5.5.38 , 5.6.0 - 5.6.38 , 7.0.0 - 7.0.33 , 7.1.0 - 7.1.25 , 7.2.0 - 7.2.33 string(71) "<tr>
<td>anything</td>
<td>4</td>
<td>4</td>
</tr>
"
string(63) "<tr>
<td></td>
<td>5</td>
<td>5</td>
</tr>
"
Output for 5.0.0 - 5.0.5 , 5.1.0 - 5.1.1 , 5.1.3 - 5.1.6 , 5.2.0 - 5.2.17 , 5.3.0 - 5.3.5 Warning: DOMDocument::saveHTML() expects exactly 0 parameters, 1 given in /in/B7KLr on line 74
NULL
Warning: DOMDocument::saveHTML() expects exactly 0 parameters, 1 given in /in/B7KLr on line 74
NULL
Output for 5.1.2 Warning: DOMDocument::saveHTML() expects exactly 0 parameters, 1 given in /in/B7KLr on line 75
NULL
Warning: DOMDocument::saveHTML() expects exactly 0 parameters, 1 given in /in/B7KLr on line 75
NULL
Output for 4.3.2 - 4.3.11 , 4.4.0 - 4.4.1 , 4.4.3 - 4.4.9 Fatal error: Cannot instantiate non-existent class: domdocument in /in/B7KLr on line 60
Process exited with code 255 . Output for 4.4.2 Fatal error: Cannot instantiate non-existent class: domdocument in /in/B7KLr on line 61
Process exited with code 255 . Output for 4.3.0 - 4.3.1 Fatal error: Cannot instantiate non-existent class: domdocument in /in/B7KLr on line 60
preferences:dark mode live preview
276.38 ms | 401 KiB | 424 Q