不要抱歉打扰我们,这是一个很好的问题,我很乐意回答。您可以使用PHP 简单 HTML DOM 解析器 http://simplehtmldom.sourceforge.net/得到你需要的东西:
$html = file_get_html('http://www.domain.com/');
$textarea = $html->find('textarea[id=body]');
$contents = $textarea->innertext;
echo $contents; // Outputs 'Hello World!'
如果你想使用file_get_contents()
,你可以这样做:
$raw_html = file_get_contents('http://www.domain.com/');
$html = str_get_html($raw_html);
...
虽然我不认为有任何必要file_get_contents()
因为你可以使用outertext
如果您需要的话,可以使用以下方法获取原始的完整 HTML:
$html = file_get_html('http://www.domain.com/');
$raw_html = $html->outertext;
只是为了好玩,您也可以使用单行正则表达式来完成此操作:
preg_match('~<textarea id="body".*?>(.*?)</textarea>~', file_get_contents('http://www.domain.com/'), $matches);
echo $matches[1][0]; // Outputs 'Hello World!'
我强烈建议不要这样做,因为你更容易受到代码更改的影响,这可能会破坏这个正则表达式。