2012-07-23 33 views
0

例如,我的網站是mysite.com。下面是該網站的源:如何使用php獲取網站內容

<html> 
<head> 
<title>site</title> 
//here is many javascript and css codes 
</head> 
<body> 
<div> 
<table border="1"> 
<tr> 
    <td><a href="somthing.html">Here is a text</td> 
    <td><img src="image.gif" alt="this is image"/></td> 
</tr> 
</table> 
</div> 
</body> 
</html> 

我如何使用PHP只能得到無所有標籤(JavaScript的代碼,鏈接,表格等)的文字和圖片? 我只想得到「這是一個文本」和「image.gif」。

+0

我真的不明白你問什麼? – Undefined 2012-07-23 12:29:42

+0

@Sam我想得到只有文本和圖像沒有標籤 – digushi 2012-07-23 12:30:24

+0

道歉,顯然是其他人做的:P – Undefined 2012-07-23 12:34:35

回答

2

如果文件位於互聯網上,請使用PHP cURL否則,如果本地計算機上有文件,則可以使用file_get_contents()函數。

爲了擺脫多餘的標籤,你可以使用代碼:

$contents - file_get_contents('file.html'); 
$contents = strip_tags($contents, '<img>'); //other than <img> you can specify more tags also 

或者你可以使用DOM方法也。