使用更好的解决方案,使用DOM:
<?php
/**
* Got this function from the manual's comments
*
* @param DOMNode $el
*
* @return mixed
*/
function innerHTML(DOMNode $el) {
$doc = new DOMDocument();
$doc->appendChild($doc->importNode($el, TRUE));
$html = trim($doc->saveHTML());
$tag = $el->nodeName;
return preg_replace('@^<' . $tag . '[^>]*>|</' . $tag . '>$@', '', $html);
}
$html = <<<HTML
<tr class="tth3">
<td>aaa - bbbbb</td>
<td>6:10 </td>
<td >bla</td>
</tr>
<tr class="tth3">
<td>cccc - xxxx</td>
<td>6:10 </td>
<td>blabla</td>
</tr>
HTML;
$document = new DOMDocument();
$document->loadHTML($html);
$tr_list = $document->getElementsByTagName("tr");
foreach ($tr_list as $tr) {
/** @var $tr DOMElement */
$td_list = $tr->getElementsByTagName("td");
foreach ($td_list as $td) {
if (preg_match("/xxxx/", $td->textContent)) {
//This is our TR!!
echo innerHTML($tr);
break(2); //Exit both loops
}
}
}