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