2014-02-16 45 views
0

我有,PHP - 如何在另一個javascript文件

// target.html 
<html xmlns="http://www.w3.org/1999/xhtml"> 
... 
<script src="../../Common/js/jquery-ui-1.10.3.js"></script> 
<script src="../../Common/js/select.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    var JsonData = [{"one":[{"ID":4179,"Name":"Jack"},{"ID":4180,"Name":"Jill"}]}]; 
... 

它包含一個JSON HTML源文件中獲取JSON,所謂的 'JsonData'。 而我想要的是在PHP中打印'JsonData'。 我讓PHP這樣,

// test.php 
include ("./target.html"); 
readfile ("./target.html"); 

$html = file_get_html("/Users/Desktop/target.html"); 
// a new dom object 
$dom = new domDocument('1.0', 'utf-8'); 
// load the html into the object ***/ 
$dom->loadHTML($html); 

$target= $dom->getElementsByTagName("script"); // here u use your desired tag  
print_r($target); 

但我不能看到這樣的JSON數據。 你能幫我嗎。

+0

無論你在做什麼,你都會以錯誤的方式去做。 –

+0

爲什麼不將json數據存儲在.json文件中並從中讀取日期? –

+0

我想直接獲取json文件,但是我不能。因爲我沒有權利。 –

回答

0

file_get_html功能不符合我的PHP存在,所以我只是用的file_get_contents,而不是它包含內置的PHP。我還將您的print_r替換爲解決問題所需的內容。您的HTML文件中有3個腳本標記,因此我使用的索引是。索引從0開始。

// test.php 
include ("./target.html"); 
readfile ("./target.html"); 

$html = file_get_contents("/Users/Desktop/target.html"); //replaced file_get_html 
// a new dom object 
$dom = new domDocument('1.0', 'utf-8'); 
// load the html into the object ***/ 
$dom->loadHTML($html); 

$target= $dom->getElementsByTagName("script"); // here u use your desired tag  

echo $target->item(2)->nodeValue; //replaced print_r 
+0

非常感謝! –

相關問題