0

I have this given:

<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>

and I will serach this regex: preg_match_all('/<tr class="tth3">.*?xxx.*?<\/[\s]*tr>/s', ...) And my result should be only the secound <tr>..</tr> , but I dont know how to use this correct so can anyone help me??

  • 4
    Welcome to Stack Overflow! Please refrain from parsing HTML with RegEx as it will [drive you insane](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). Use an [HTML parser](http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php) instead. – Madara's Ghost Jun 25 '12 at 16:05

2 Answers2

2

Using a better solution, with 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
        }
    }
}
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

I don't think putting the \s class in brackets is necessary and it might even be interpreted as something other than the space class. I'm not 100% sure, though.

[\s]

Either way, the usage is:

$num_matches = preg_match_all( '/<tr class="tth3">.*?xxx.*?<\/\s*tr>/s', $subject, $matches );

  1. $num_matches contains the count of matched strings
  2. $matches contains an array of the actual matched strings
efesar
  • 1,323
  • 2
  • 9
  • 9