簡體   English   中英

PHP僅獲得html代碼的一部分

[英]PHP get only one section of the html code

我在Codeplex的項目站點上有文檔,並且我想創建一個PHP代碼以將其嵌入到我的站點中。 這是我想從頁面獲取的代碼的一部分。 如果您能幫助我,那就太好了。

這是我想從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>

您可以在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'));

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標簽? 如果您不希望標簽檢查我的第一版答案。
EDIT2:對不起,當我對其進行測試時,它不起作用,但現在可以了。
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM