简体   繁体   English

PHP仅获得html代码的一部分

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

I have documentation on my project site on codeplex, and i want to create a PHP code to embed it to my site. 我在Codeplex的项目站点上有文档,并且我想创建一个PHP代码以将其嵌入到我的站点中。 and this is the part of code i want to get from the page. 这是我想从页面获取的代码的一部分。 If you could help me it would be great. 如果您能帮助我,那就太好了。

This is the code i want to extract from https://mxspli.codeplex.com/documentation : 这是我想从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>

You can use DOMDocument in PHP. 您可以在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 will find the position of the string 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;

Edit: I just noticed you wanted the html tags? 编辑:我只是注意到您想要html标签? If you don't want the tags check my first version of this answer. 如果您不希望标签检查我的第一版答案。
EDIT2: sorry it didn't work when I tested it, but now it does. EDIT2:对不起,当我对其进行测试时,它不起作用,但现在可以了。
... ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM