簡體   English   中英

preg_match 問題

[英]preg_match problem

我有索引。html

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

我需要在 body 標簽內獲取內容。 試過這個

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

但它不是 output 到任何東西。 請在這里告訴我問題。 謝謝你。

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

這可能不是您的答案,但我建議您嘗試 php DOMDocument鏈接

"/<body[^>]*>(.*?) \/body>/is"應該是"/<body[^>]*>(.*?)<\/body>/is"

你應該看看 PHP Simple HTML DOM Parser: http://simplehtmldom.sourceforge.net/

你可以用這樣的東西得到身體:

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

然后,您可以通過以下方式獲得內部 HTML:

$content = $body->innertext;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM