首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从同一服务器上的另一个页面提取特定div的内容?

如何从同一服务器上的另一个页面提取特定div的内容?
EN

Stack Overflow用户
提问于 2011-01-08 18:24:16
回答 3查看 2.1K关注 0票数 1

你好,朋友们,我正在同一台服务器上运行两个应用程序,我想将Application-1的一些内容导入Application-2,类似地,将Application-2的一些内容导入Application-2。

目前我就是这样得到它的:

代码语言:javascript
复制
<?php
    $app1 = file_get_contents('http://localhost/cms');

    file_put_contents('cms.html', $app1); 

    $app2 = file_get_contents('http://localhost/blog');

    file_put_contents('blog.html', $app2); 
?>

就像这样,我用这些应用程序的HTML保存这两个文件

现在让我们说我有

<div id="header"> in cms.html,我想在<div id="header"> of Application-2的index.php file中导入它。

<div id="footer"> in blog.html,我想要在<div id="footer"> of Application-1的index.php文件中。

我该怎么做?

更新: --这两个应用程序都是Joomla和WordPress。

我的模板是一个简单的没有任何JavaScript的模板,所以友好地建议一个JavaScript解决方案,直到并且除非加载JavaScript是强制性的,否则我想保持简单和轻巧。

也请注意,我刚刚开始学习编程,因此,我将很难理解太技术性的语言,因此要求一个简单的语言。希望你能理解。

如果我的方法一开始就错了,那么请尽管提出一个更好的方法来完成这个任务。

帮帮忙吧。

更新2:请不要我的问题是从其他应用程序获取内容。我的问题是获取正在生成的HTML文件的片段。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-01-08 20:57:31

嗯,我想我自己找到了一个更好的方法,所以我在这里张贴作为一个答案,因为其他人正在寻找一个类似的解决方案。

编写一个类,该类将在标记之间读取并得到该位。

代码语言:javascript
复制
<?Php
class className {
    private $_content;
    private $_Top;
    private $_Bottom;

    public function __construct(
        $url, $markerStartTop = null, 
        $markerEndTop = null, 
        $markerStartBottom = null, 
        $markerEndBottom = null){

            $this->_content = file_get_contents($url);
            $this->_renderTop($markerStartTop, $markerEndTop);
            $this->_renderBottom($markerStartBottom, $markerEndBottom);
        }

    public function renderTop($markerStart = null, $markerEnd = null){
        return $this->_Top;
        }

    private function _renderTop($markerStart = null, $markerEnd = null){
        $markerStart = (is_null($markerStart)) ? '<!– Start Top Block –>' : (string)$markerStart;
        $markerEnd = (is_null($markerEnd)) ? '<!– End Top Block –>' : (string)$markerEnd;

        $TopStart = stripos($this->_content, $markerStart);
        $TopEnd = stripos($this->_content, $markerEnd);

        $this->_Top = substr($this->_content, $TopStart, $TopEnd-$TopStart);
        }

    public function renderBottom(){
        return $this->_Bottom;
        }

    private function _renderBottom($markerStart = null, $markerEnd = null){ 
        $markerStart = (is_null($markerStart)) ? '<!– Start Bottom Block –>' : (string)$markerStart;
        $markerEnd = (is_null($markerEnd)) ? '<!– End Bottom Block –>' : (string)$markerEnd;

        $BottomStart = stripos($this->_content, $markerStart);
        $BottomEnd = stripos($this->_content, $markerEnd);

        $this->_Bottom = substr($this->_content, $BottomStart, $BottomEnd-$BottomStart);
        }
}
?>

称之为:

代码语言:javascript
复制
<?php
    $parts = new className('url/to/the/application');
    echo $parts->renderTop();
?>

此方法准确地获取您在所需标记之间标记的内容的片段。

票数 0
EN

Stack Overflow用户

发布于 2011-01-08 18:53:40

如果文件驻留在同一个本地服务器上,则不需要在HTTP中使用file_get_contents。这样做将执行HTTP请求,这会增加不必要的延迟。

除非您希望由它们各自的应用程序处理这些文件,否则可以简单地从它们的文件路径加载HTML文件的内容。

代码语言:javascript
复制
$content = file_get_contents('/path/to/application1/html/file.htm');

如果这些文件包含需要评估的PHP,请使用

代码语言:javascript
复制
include 'path/to/application1/html/file.htm';

而不是。请注意,使用include将使PHP在目标文件的开头进入HTML模式,并在结束时再次恢复PHP模式。因此,必须用适当的<?php打开标记将任何PHP代码括在文件中。

如果需要捕获变量中包含调用的输出,请将其包装到

代码语言:javascript
复制
ob_start();
include '…'; // this can also take a URL
$content = ob_get_clean();

这将启用输出缓冲

如果需要处理文件中的HTML,请使用适当的HTML解析器

票数 0
EN

Stack Overflow用户

发布于 2011-01-08 21:30:17

这假定了格式良好的XHTML和PHP 5:

  1. 通过某种方法(例如cURL请求)获得所需的HTML。
  2. 使用SimpleXML将XML解析为DOM,然后使用XPath提取所需的节点。
  3. 以文本形式发出提取的节点。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4635432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档