2016-04-29 68 views
-1

我在我的項目網站上有關於codeplex的文檔,我想創建一個PHP代碼將其嵌入到我的網站。和 this is the part of code i want to get from the page. 如果你能幫助我,這將是偉大的。PHP只獲取html代碼的一部分

這是我想從https://mxspli.codeplex.com/documentation提取代碼:

<div id="WikiContent" class="WikiContent"> 

<div class="wikidoc"> 
<h1><img src="https://download-codeplex.sec.s-msft.com/Download?ProjectName=mxspli&amp;DownloadId=1564842&amp;Build=21031" alt="" width="101" height="23">&nbsp;Documentation</h1> 
<p>Welcome to MXSPLI Documentation</p> 
<p>Here you can learn how to use MXSPLI, and how to make libraries for it.</p> 
<ul> 
<li><a href="https://mxspli.codeplex.com/wikipage?title=Tutorials">Tutorials</a> </li><li><a href="https://mxspli.codeplex.com/wikipage?title=Released%20Libraries">Released Libraries</a> 
</li><li><a href="https://mxspli.codeplex.com/wikipage?title=Main%20Functions">Main Functions</a> 
</li></ul> 
</div> 
<div></div> 
</div> 

回答

1

Strpos將查找字符串

$string = file_get_contents('https://mxspli.codeplex.com/documentation'); 
$pos = strpos($string,'<div id="WikiContent" class="WikiContent">'); 
$pos2 = strpos($string, "<div></div>", $pos); 
$newstring = substr($string, $pos, ($pos2-$pos)); 
Echo $newstring; 

編輯的位置:我剛纔注意到你想要的html標籤?如果你不希望標籤檢查我的第一個版本的答案。
編輯2:抱歉,當我測試它時,它不工作,但現在它。
...

+0

感謝您發表本文。 –

+0

這裏我們再去... [你不能用正則表達式解析HTML](http://stackoverflow.com/a/1732454/797495) –

+0

@PedroLobito對不起?正則表達式在哪裏? – Andreas

1

可以在PHP中使用DOMDocument

<?php 
$pageHtml = file_get_contents('https://mxspli.codeplex.com/documentation'); 
$document = new DOMDocument(); 
@$document->loadHtml($pageHtml); 

/* Write out the HTML snippet */ 
echo $document->saveHTML($document->getElementById('WikiContent')); 
+0

它的工作原理,但我先測試@安德烈亞斯的代碼。抱歉。 –

+0

我測試了你的代碼,它不工作,我得到 '警告:DOMDocument :: saveHTML()期望0參數,1在/ home/...中給出,沒有任何進一步的數據。 –

+1

@PedroLobito您正在使用過時的PHP版本。這將適用於PHP 5.3.6或更高版本。 –