简体   繁体   English

PHP从中提取文本 <pre> ?

[英]PHP Extracting text from <pre>?

I have a .html file which looks like this: 我有一个.html文件,看起来像这样:

<html>
<body>
<pre style="word-wrap: break-word; white-space: pre-wrap;">{blah blah blah text that I want extracted} </pre>
</body>
</html>

I want to make a PHP script that will extract the inner text from the <pre> tag. 我想制作一个PHP脚本,它将从<pre>标签中提取内部文本。 I have tried Simple HTML DOM and that hasn't done a single thing it just echos out nothing. 我尝试过简单的HTML DOM,并没有完成任何事情,它只是回忆一下。 Im not sure what im doing wrong here or wether or not I should be using Simple HTML Dom. 我不知道我在这里做错了什么或者不管我是不是应该使用Simple HTML Dom。 Heres my code for the PHP script to extract the text from the html file: 继承我的PHP脚本代码,从html文件中提取文本:

<?php
include 'simple_html_dom.php';
// Create DOM from URL or file
$html = file_get_html('http://data.mtgox.com/api/2/BTCUSD/money/ticker');

// Find all images
foreach($html->find('img') as $element)
       echo $element->src . '<br>';

// Find all links
foreach($html->find('pre') as $element)
       echo $element . '<br>';
?>

Any ideas on why its not working? 关于它为什么不起作用的任何想法?

working DOMDOcument example 工作DOMDOcument示例

$doc = new DOMDocument();
$doc->loadXML('<html><body><pre style="word-wrap: break-word; white-space: pre-wrap;">{blah blah     blah text that I want extracted} </pre></body></html>');
$pres = $doc->getElementsByTagName('pre');
foreach($pres as $v){
   echo $v->firstChild->wholeText.'<br />';
}

The URL you are fetching is returning JSON, not HTML. 您要获取的URL是返回JSON,而不是HTML。

Use a json parser to handle it. 使用json解析器来处理它。

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

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