Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP从不同页面获取文本_Php_Html - Fatal编程技术网

使用PHP从不同页面获取文本

使用PHP从不同页面获取文本,php,html,Php,Html,如何使用PHP从其他(本地)页面获取一些html 我有一个页面“Product1.htm”,我希望找到div中的文本,该文本的类为“price”,然后显示在另一个页面上:“Products\u overview.htm”。所以在Products_overview.htm中,我猜是PHP,它以product1.htm为目标,显示“Price”DIV的内容 我不知道该怎么做,但我相信这一定是令人尴尬的简单!有什么帮助吗?您可以使用文件\u获取\u内容('http://..../Product1.ht

如何使用PHP从其他(本地)页面获取一些html

我有一个页面“Product1.htm”,我希望找到div中的文本,该文本的类为“price”,然后显示在另一个页面上:“Products\u overview.htm”。所以在Products_overview.htm中,我猜是PHP,它以product1.htm为目标,显示“Price”DIV的内容


我不知道该怎么做,但我相信这一定是令人尴尬的简单!有什么帮助吗?

您可以使用
文件\u获取\u内容('http://..../Product1.html)
或将其解析为普通文本。

严格地说,最好是弄清楚div“price”的内容是如何生成的,然后在Products_overview.htm中重现。如果无法执行此操作,则可以使用CURL:

假设PHP需要在输出div之前解析.htm文件:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://localhost/path/to/Product1.htm"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
$content = curl_exec($ch); 
if(preg_match('/<div class=\'Price\'>(.*?)<\/div>/is', $content, $matches) { 
    echo $matches[1]; 
}
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,”http://localhost/path/to/Product1.htm"); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
$content=curl\u exec($ch);
如果(preg_match('/(.**?)/is',$content,$matches){
echo$匹配[1];
}
简单方法:使用:

示例:

Page.html

Some html...

<div class="price">
Vivamus malesuada hendrerit metus, eu viverra odio viverra nec. Maecenas nec felis est, sit amet molestie massa. Morbi odio dolor, scelerisque eget bibendum et, volutpat non risus. Curabitur eleifend, lacus non rutrum sollicitudin, est diam fermentum nisl, vel lacinia felis felis quis odio. Aliquam mollis, est nec porttitor feugiat, velit risus dapibus dolor, ac viverra tortor
</div>

Some html...
一些html。。。
我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们,我的朋友们皮布斯·多洛,ac维韦拉托托
一些html。。。
PHP文件:

<?php
include 'simple_html_dom.php';
$html = file_get_html('page.html');

echo $html->find("div[class=price]", 0); // will echo content inside a <div class="price"> </div>
?>

使用cURL(根据@cegfault的回答)。但是,要获取文本本身,请使用类似于DOMDocument的HTML解析器

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://localhost/path/to/Product1.htm"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
$content = curl_exec($ch); 
$doc = new DOMDocument();
$doc->loadHTML($content);
$finder = new DomXPath($doc);
$classname="price";
$nodes = $finder->query("//div[contains(@class, '$classname')]");

如果你愿意使用JavaScript而不是非常简单的PHP,你可能会想签出这个SO线程。我甚至不知道它的存在!非常感谢!哇!我没有想过使用file_get_html。我只知道我不能将find(非成员)与file_get_内容一起使用。这使得它比curl更简单、更干净。div的内容“price”是由我手工输入的!我的想法是,这里的任何更改都将反映在其他显示价格的页面上。cURL看起来很有希望,但我被简单的HTML DOM解决方案的简单性所吸引。我将进一步研究cURL!谢谢