开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜