要取出 PHP 中的 table 标签,可以使用正则表达式或者 DOM 解析器。以下是两种方法的示例:
- 使用正则表达式:
$html = '<table><tr><td>Cell 1</td><td>Cell 2</td></tr></table>';
preg_match('/
(.*?)<\/table>/s', $html, $matches);
if (isset($matches[0])) {
$tableHtml = $matches[0];
echo $tableHtml;
} else {
echo "Table tag not found";
}
2. 使用 DOM 解析器:
```php
$html = '<table><tr><td>Cell 1</td><td>Cell 2</td></tr></table>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$tables = $dom->getElementsByTagName('table');
if ($tables->length > 0) {
$table = $tables->item(0);
$tableHtml = $dom->saveHTML($table);
echo $tableHtml;
} else {
echo "Table tag not found";
}
无论使用哪种方法,都可以将 table 标签的内容提取出来并打印出来。
上一篇:php更改css无法应用(php的css样式如何使用)
下一篇:php中的定界符(php定义字符串的三种方式)