2012-11-06 92 views
0

我試圖找出是否有可能&使用什麼樣的代碼:加載當前頁面的內容和回聲出一個相對路徑到一個特定的頁面(c.html)這是瓦特/ 「#navbar一個」 使用PHP或PHP Simple Html DOM Parser.PHP獲取當前頁面的具體內容

到目前爲止我的代碼:

<?php 
$pg = 'c.html'; 
include_once '%resource(simple_html_dom.php)%'; 
/* $cpath = $_SERVER['REQUEST_URI']; Old version */ // Path to current pg from root 
$cpath = "http://www.".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; 
echo var_dump($cpath).": Current Root Path"."<br />"; // "http://www.partiproductions.com/copyr/index.php" - Correct 
$cfile = basename($cpath); 
echo 'Current File: ' . $cfile . "<br />"; // "index.php" - Current pg, Correct 

$html = file_get_html($cpath); // Getting Warning: URL file-access is disabled in the server configuration & failed to open stream: no suitable wrapper could be found in.. & Fatal error: Call to a member function find() on a non-object in... 
foreach($html->find(sprintf('#navbar a[href=%s]', $pg)) as $path) { 
    echo 'Path: ' . $path."<br />"; 
} 
?> 
+0

做'的print_r($ _ SERVER)',看看是否有任何值那裏,你可以使用。 – Landon

+0

嘗試'$ html = file_get_html('index.php');'看看你是否有任何東西。如果沒有,是否與您的腳本在同一個目錄中是'index.php'?如果沒有,請嘗試獲取'$ cpath'。 – cpilko

+0

嗨@cpilko試圖只是index.php,但沒有奏效。 index.php是當前頁面。在 – parti

回答

1

你的主要問題是與您的來電file_get_html($ CFile的)。在你的榜樣

$的CFile將包含類似/copyr/index.php

當你通過這個進file_get_html(),它要尋找你的服務器的根目錄的目錄/ copyr,和裏面有一個index.php文件。根據您所指出的警告,您實際上並未在服務器的根目錄中擁有此文件夾結構。

你真正需要做的是包括在URI前面的完整URL您當前擁有,這樣的:

$cpath = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; 

這將導致在這樣的路徑:http://www.yourserver.com/copyr/index.php,你應該再努力for file_get_html();

+0

好的Wally,我用http:// www。在開始時,它現在輸出當前路徑爲http:// www.partiproductions.com/copyr/index.php,但它是一個字符串 - 會導致問題嗎?這個php腳本在當前頁面上,因爲我沒有明確說明。獲取:警告:file_get_contents()[function.file-get-contents]:文件名不能爲空... – parti

+0

使用cpath獲取:Warning:file_get_contents()[function.file-get-contents]:URL文件訪問是在服務器配置中禁用.. AND file_get_contents(http://www.partiproductions.com/copyr/index.php)[function.file-get-contents]:未能打開流:沒有找到合適的包裝器。 – parti

0

基於從提問者的更新信息,我會按照不同的方法。

創建僅包含要跨這兩個文件共享內容的新文件。然後,在這兩個文件中(或更高版本,如果需要)使用include()函數從新的共享內容文件注入內容。

index.php文件:

<?php 
//Any require PHP code goes here 
?> 
<html> 
    <body> 
    <?php include('sharedfile.php');?> 
    </body> 
</html> 

/copyr/c.php文件:

<?php 
//Any require PHP code goes here 
?> 
<html> 
    <body> 
    <?php include('../sharedfile.php');?> 
    </body> 
</html> 

sharedfile.php:

// You need to close the PHP tag before echoing HTML content 
?> 
<p> 
    This content is displayed via include() both on index.php and /copyr/c.php 
</p> 
<?php // The PHP tag needs to be re-opened at the end of your shared file 

釷這裏的好處是,您現在可以通過遵循相同的技術,在您網站上的任何文件中使用sharedfile.php文件內容。您也不需要解析頁面的DOM,只需刪除要在多個頁面上顯示的內容,這些內容可能很慢並且容易出錯。

+0

現在讓我們留在w/just index&c.html/php文件中。在c文件中,我希望能夠定位#myid或.myclass並將特定內容導入到index.php中。 c.php的路徑需要自動找到。在你的方法中,有一個文件w /只有內容需要,我不能做 - 必須從一個更大的現有文件(c.html/php) - 拉謝謝 – parti