Php 从包含html代码的字符串中提取html节
我会从字符串中提取一些html代码。 此字符串从php joomla页面返回。 此字符串包含如下代码:Php 从包含html代码的字符串中提取html节,php,regex,joomla,Php,Regex,Joomla,我会从字符串中提取一些html代码。 此字符串从php joomla页面返回。 此字符串包含如下代码: <!-- JoomlaWorks "Disqus Comment System for Joomla!" Plugin (v2.2) starts here --> <div class="itp-fshare-floating" id="itp-fshare" style="position:fixed; top:30px !important; left:50px !i
<!-- JoomlaWorks "Disqus Comment System for Joomla!" Plugin (v2.2) starts here -->
<div class="itp-fshare-floating" id="itp-fshare" style="position:fixed; top:30px !important; left:50px !important;">
</div>
<p>
<span class="easy_img_caption" style="display:inline-block;line-height:0.5;vertical-align:top;background-color:#F2F2F2;text-align:left;width:180px;float:left;margin:0px 10px;">
<a href="/joomla/index.php?option=com_content&view=article&id=13:11111&catid=1:guide-sui-serivzi-cloud-computing&Itemid=3">
<img src="/joomla/plugins/content/imagesresizecache/441a27b2a4d64b487a8e213a94f6466d.jpeg" border="0" alt="1" title="1" style="width:180px; height:150px; ;margin:0;" />
</a>
<span class="easy_img_caption_inner" style="display:inline-block;line-height:normal;color:#000000;font-size:8pt;font-weight:normal;font-style:normal;padding:4px 8px;margin:0px;">1
</span>
</span>
11111111111111111111111111111111111
</p>
<!-- Disqus comments counter and anchor link -->
<a class="jwDisqusListingCounterLink" href="http://clouderize.it/joomla/index.php?option=com_content&view=article&id=13:11111&catid=1:guide-sui-serivzi-cloud-computing&Itemid=3#disqus_thread" title="Add a comment">
Add a comment
</a>
<!-- JoomlaWorks "Disqus Comment System for Joomla!" Plugin (v2.2) ends here -->
1.
11111111111111111111111111111111111
我将摘录这一节:
<span class="easy_img_caption" style="display:inline-block;line-height:0.5;vertical-align:top;background-color:#F2F2F2;text-align:left;width:180px;float:left;margin:0px 10px;">
<a href="/joomla/index.php?option=com_content&view=article&id=13:11111&catid=1:guide-sui-serivzi-cloud-computing&Itemid=3">
<img src="/joomla/plugins/content/imagesresizecache/441a27b2a4d64b487a8e213a94f6466d.jpeg" border="0" alt="1" title="1" style="width:180px; height:150px; ;margin:0;" />
</a>
<span class="easy_img_caption_inner" style="display:inline-block;line-height:normal;color:#000000;font-size:8pt;font-weight:normal;font-style:normal;padding:4px 8px;margin:0px;">1
</span>
</span>
1.
我该怎么办?
非常感谢
修改1:
$content="<html><head></head><body>".($this->item->text)."</body></html>";
$dom = new DOMDocument();
$dom->loadHTML($content);
$xpath = new DOMXPath($dom);
$tags = $xpath->query('//span[@class="easy_img_caption"]/');
print_r($tags);
$content=“”($this->item->text)。”;
$dom=新的DOMDocument();
$dom->loadHTML($content);
$xpath=newdomxpath($dom);
$tags=$xpath->query('//span[@class=“easy\u img\u caption”]/');
打印(标签);
修改2:
使用此代码:
$content="<html><head></head><body>".($this->item->text)."</body></html>";
$content=($this->item->text);
$dom = new DOMDocument();
$dom->loadHTML($content);
$xpath = new DOMXPath($dom);
$tags = $xpath->query('//span[@class="easy_img_caption"]');
//echo "<textarea>".print_r($dom->saveXml($tags->item(0)))."</textarea>";
foreach ($tags as $tag) {
$innerHTML = '';
$children = $tag->childNodes;
foreach ($children as $child) {
$tmp_doc = new DOMDocument();
$tmp_doc->appendChild($tmp_doc->importNode($child,true));
$innerHTML .= $tmp_doc->saveHTML();
}
echo $innerHTML;
$content=“”($this->item->text)。”;
$content=($this->item->text);
$dom=新的DOMDocument();
$dom->loadHTML($content);
$xpath=newdomxpath($dom);
$tags=$xpath->query('//span[@class=“easy\u img\u caption”]”);
//echo“.print_r($dom->saveXml($tags->item(0))”;
foreach($tags作为$tag){
$innerHTML='';
$children=$tag->childNodes;
foreach($childrenas$child){
$tmp_doc=新的DOMDocument();
$tmp_doc->appendChild($tmp_doc->importNode($child,true));
$innerHTML.=$tmp_doc->saveHTML();
}
echo$innerHTML;
我回来了:
<a href="/joomla/index.php?option=com_content&view=article&id=13:11111&catid=1:guide-sui-serivzi-cloud-computing&Itemid=3">
<img src="/joomla/plugins/content/imagesresizecache/441a27b2a4d64b487a8e213a94f6466d.jpeg" border="0" alt="1" title="1" style="width:180px; height:150px; ;margin:0;">
</a>
<span class="easy_img_caption_inner" style="display:inline-block;line-height:normal;color:#000000;font-size:8pt;font-weight:normal;font-style:normal;padding:4px 8px;margin:0px;">1</span>
1.
问题是我还想要上一个跨度:
<span class="easy_img_caption" style="display:inline-block;line-height:0.5;vertical-align:top;background-color:#F2F2F2;text-align:left;width:180px;float:left;margin:0px 10px;">
在xpath查询中我必须做什么
再次感谢。以下是使用PHPQuery库为您的测试字符串提供的解决方案:
(相关)我不太擅长dom和xmlreader…你能帮我编写一些代码吗?当然。看看我的xpath查询是否有错?删除尾部斜杠并使用$dom->saveXml($tags->item(0))打印;
。查询方法返回一个DOMNodeList。另外,当您使用loadHTML时,您不必在$this->item周围添加HTML框架,因为DOM会自动添加它。您好,但使用DOM我该怎么做?谢谢,我很感激,但我不希望使用外部libs。
<?php
require('phpQuery/phpQuery.php');
$testString =
'<!-- JoomlaWorks "Disqus Comment System for Joomla!" Plugin (v2.2) starts here -->
<div class="itp-fshare-floating" id="itp-fshare" style="position:fixed; top:30px ! important; left:50px !important;">
</div>
<p>
<span class="easy_img_caption" style="display:inline-block;line-height:0.5;vertical-align:top;background-color:#F2F2F2;text-align:left;width:180px;float:left;margin:0px 10px;">
<a href="/joomla/index.php?option=com_content&view=article&id=13:11111&catid=1:guide-sui-serivzi-cloud-computing&Itemid=3">
<img src="/joomla/plugins/content/imagesresizecache/441a27b2a4d64b487a8e213a94f6466d.jpeg" border="0" alt="1" title="1" style="width:180px; height:150px; ;margin:0;" />
</a>
<span class="easy_img_caption_inner" style="display:inline-block;line-height:normal;color:#000000;font-size:8pt;font-weight:normal;font-style:normal;padding:4px 8px;margin:0px;">1
</span>
</span>
11111111111111111111111111111111111
</p>
<!-- Disqus comments counter and anchor link -->
<a class="jwDisqusListingCounterLink" href="http://clouderize.it/joomla/index.php?option=com_content&view=article&id=13:11111&catid=1:guide-sui-serivzi-cloud-computing&Itemid=3#disqus_thread" title="Add a comment">
Add a comment
</a>
<!-- JoomlaWorks "Disqus Comment System for Joomla!" Plugin (v2.2) ends here -->';
$doc = phpQuery::newDocument($testString);
$extraction=pq('.easy_img_caption:eq(0)')->htmlOuter();
echo $extraction;
/* outputs
<span class="easy_img_caption" style="display:inline-block;line-height:0.5;vertical- align:top;background-color:#F2F2F2;text-align:left;width:180px;float:left;margin:0px 10px;">
<a href="/joomla/index.php?option=com_content&view=article&id=13:11111&catid=1:guide-sui-serivzi-cloud-computing&Itemid=3">
<img src="/joomla/plugins/content/imagesresizecache/441a27b2a4d64b487a8e213a94f6466d.jpeg" border="0" alt="1" title="1" style="width:180px; height:150px; ;margin:0;"></a>
<span class="easy_img_caption_inner" style="display:inline-block;line-height:normal;color:#000000;font-size:8pt;font-weight:normal;font-style:normal;padding:4px 8px;margin:0px;">1
</span>
</span>
*/
?>