开发者

How can I grab the entire content inside `<body>` tag with regex?

开发者 https://www.devze.com 2023-03-24 08:17 出处:网络
How can I grab the entire content inside <body> tag with regex? For instance, <html><body><p><a href=\"#\">xx</a></p>

How can I grab the entire content inside <body> tag with regex?

For instance,

<html><body><p><a href="#">xx</a></p>

<p><a href="#">xx</a></p></body></html> 

I want to return this only,

<p><a href="#">xx</a></p>

<p><a href="#">xx</a></p>

Or any other better ideas? maybe DOM but I have to use saveHTML(); then it will return doctype and body tag...

HTML Pu开发者_JS百科rifier is a pain to use so I decide not to use it. I thought regex could be the next best option for my disaster.


preg_match("/<body[^>]*>(.*?)<\/body>/is", $html, $matches);

$matches[1] will be the contents of the body tag


preg_match("~<body.*?>(.*?)<\/body>~is", $html, $match);
print_r($match);
0

精彩评论

暂无评论...
验证码 换一张
取 消