DOMDocumentの使用
このようなものには正規表現を使用しないでください。むしろ、マークアップからDOMDocumentを作成してから、その特定の要素から子を選択します。たとえば、次のようにすると<td>、マークアップ内の各タグの集合的なhtmlが得られます。
// Our HTML will eventually go here
$innerHTML = "";
// Create a new DOMDocument based on our HTML
$document = new DOMDocument;
$document->loadHTML($html);
// Get a NodeList of all <td> Elements
$cells = $document->getElementsByTagName("td");
// Cycle over each <td>, adding its HTML to $innerHTML
foreach ($cells as $cell) {
$innerHTML .= $document->saveHTML($cell);
}
// Output our glorious HTML
echo $innerHTML;
正規表現
trを使用してタグの間にあるものを本当に取得したい場合はpreg_match、次のように機能するはずです。
// Our pattern for capturing all that is between <tr> and </tr>
$pattern = "/<tr[^>]*>(.*)<\/tr>/s";
// If a match is found, store the results in $match
if (preg_match($pattern, $html, $match)) {
// Show the captured value
echo $match[1];
}
その結果、次のようになります。
<td class="alt1 eventDate smallfont" align="center"></td>
<td class="alt1 smallfont" align="center">3:34am</td>
<td class="alt1 smallfont" align="center">CNY</td>