简体   繁体   English

PHP HTML preg_match_all

[英]PHP HTML preg_match_all

<div class="comment">
  <div class="pros">text1</div>
   <noindex>
    <div class="contras">text2</div>
   </noindex>
   <p>text3</p>
   <a few tags...>
</div>

 <div class="comment">
      <div class="pros">text1</div>
       <noindex>
        <div class="contras">text2</div>
       </noindex>
       <p>text3</p>
       <a few tags...>
    </div>

how to get the contents of the blocks ? 如何获得块的内容?

text1 text2 文字1文字2

text3 文字3

 preg_match_all ('/<div class=\\"comment\\"><div class=\\"pros\\">(.*?)<\\/div><noindex><div class=\\"contras\\">(.*?)<\\/div><\\/noindex><p>(.*?)<\\/p><\\/div>/Uisu',$content,$found4); 

Parser does not offer 解析器不提供

You can use the html-parser library aswell. 您也可以使用html-parser库。

http://htmlparser.sourceforge.net/samples.html http://htmlparser.sourceforge.net/samples.html

<?php
 $html = new DOMDocument();
 @$html->loadHTML($source)
 $xpath = new DOMXPath( $html );
 $proslist = $xpath->query( "//*[contains(@class, 'pros')]" );
 foreach ($proslist as $list)
 {
    echo $list->nodeValue."\n";
 }
?>

This will help you in extracting the text1 specified in the div with class pros .$source represents your html.As said in the previous comment you have to look in parsing html with php and I hope this Reference link for parsing html with php will help you. 这将有助于您使用pros类提取div中指定的text1 。$ source表示您的html。如前一条评论中所述,您必须查看使用php解析html,希望此参考链接可帮助您使用php解析html。您。

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

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