简体   繁体   English

preg_match 问题

[英]preg_match problem

I have index.html我有索引。html

<html> <head> bla bla bla </head> <body class="someclass"> bla bla bla </body> </html>

I need get content inside body tag.我需要在 body 标签内获取内容。 Tried this试过这个

<?php $site = file_get_contents("index.html"); preg_match("/<body[^>]*>(.*?) \/body>/is", $site, $matches); print ($matches[1]); ?>

But it not output to anything.但它不是 output 到任何东西。 Please tell me problem here.请在这里告诉我问题。 Thank you.谢谢你。

<?php 
$site = file_get_contents("index.html"); 
preg_match("/<body.*?>(.*?)<\/body>/is", $site, $matches); 
print ($matches[1]); 
?>

It may be not your answer but i recommend you to try php DOMDocument link这可能不是您的答案,但我建议您尝试 php DOMDocument链接

"/<body[^>]*>(.*?) \/body>/is" Should be "/<body[^>]*>(.*?)<\/body>/is" "/<body[^>]*>(.*?) \/body>/is"应该是"/<body[^>]*>(.*?)<\/body>/is"

You should take a look at PHP Simple HTML DOM Parser: http://simplehtmldom.sourceforge.net/你应该看看 PHP Simple HTML DOM Parser: http://simplehtmldom.sourceforge.net/

You can get the body with something like this:你可以用这样的东西得到身体:

$html = file_get_html('index.html')
$body = $html->find('body');

you can then get the inner HTML by:然后,您可以通过以下方式获得内部 HTML:

$content = $body->innertext;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM