Match the website <body> For tags and their content, you can use the following regular expression:
$pattern = '/<body[^>]*>(.*?)<\/body>/is';This regular expression matches. <body> Tag and its internal content, where .*? Represents non-greedy matching of any character.
Here is an example code:
// 获取目标 URL 的 HTML 内容
$html = file_get_contents('https://example.com');
// 匹配 body 部分
$pattern = '/<body[^>]*>(.*?)<\/body>/is';
if (preg_match($pattern, $html, $matches)) {
// 输出 body 内容
echo $matches[1];
} else {
echo '没有找到 body';
}In the above code, we use preg_match() Use a function for matching <body> The tag and its content are processed, and the matching results are output; if no match is found, <body> If the tag is found, output "Body not found".