[使用php解析器获取带有类的p标记中的所有子项

问题描述 投票:0回答:1

我正在使用简单的php解析器,有很多带有faq-d-item-content easeParent类的div,并且里面有一个带有h5标签的问题。还有带有faq-popup类的ap标签。问题的答案不是在统一类或标签中。一些答案在div标签中,而ul中的ul在ap标签中,是类faq-popup。问题是我收到所有问题,但由于答案中使用了“&nbsp”,因此无法获得完整答案。

这是示例[[HTML。

<div class="faq-d-item-content easeParent"> <h5>What are the KYC requirements for opening a bank account?</h5> <p class="faq-popup"> <p> To open a bank account, one needs to submit a ‘proof of identity and proof of address' together with a recent photograph.</p> </p> </div> <div class="faq-d-item-content easeParent"> <h5>What are the documents to be given as ‘proof of identity' and ‘proof of address'?</h5> <p class="faq-popup"> <p> The Government of India has notified six documents as ‘Officially Valid Documents (OVDs) for the purpose of producing proof of identity. These six documents are Passport, Driving Licence, Voters' Identity Card, PAN Card, Aadhaar Card issued by UIDAI and NREGA Card. You need to submit any one of these documents as proof of identity. If these documents also contain your address details, then it would be accepted as ‘proof of address'. If the document submitted by you for proof of identity does not contain address details, then you will have to submit another officially valid document which contains address details.</p> </p> </div> <div class="faq-d-item-content easeParent"> <h5>If I do not have any of the documents listed above to show my ‘proof of identity', can I still open a bank account?</h5> <p class="faq-popup"> <p> Yes. You can still open a bank account known as ‘Small Account' by submitting your recent photograph and putting your signature or thumb impression in the presence of the bank official.</p> </p> </div> <div class="faq-d-item-content easeParent"> <h5>Is there any difference between such ‘small accounts' and other accounts ?</h5> <p class="faq-popup"> <p> Yes. The ‘Small Accounts' have certain limitations such as:</p> <p style="margin-left:1.0cm;"> ·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; balance in such accounts at any point of time should not exceed Rs.50,000</p> <p style="margin-left:1.0cm;"> ·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; total credits in one year should not exceed Rs.1,00,000</p> <p style="margin-left:1.0cm;"> ·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; total withdrawal and transfers should not exceed Rs.10,000 in a month.</p> <p style="margin-left:1.0cm;"> ·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Foreign remittances cannot be credited to such accounts.</p> <p> Such accounts remain operational initially for a period of twelve months and thereafter, for a further period of twelve months, if the holder of such an account provides evidence to the bank of having applied for any of the officially valid documents within twelve months of the opening of such account.&nbsp;</p> </p> </div>

php代码

$html = file_get_html(url ); $content = $html->find( 'div[class=faq-d-item]',0) $questions = $content->find( 'div[class=faq-d-item-content easeParent] h5' ); foreach($questions as $k => $question){ $question_array[] = $question->innertext; } $answers = $html->find( 'div[class=faq-d-item-content easeParent] p[class=faq-popup]'); foreach ($answers as $ky => $node){ $answer_array[] =$node->plaintext; }
我想用类faq-popup作为数组在p标记中获取所有子代。还需要解决“&nbsp”解析时返回空值的解决方案。    
php html parsing simple-html-dom
1个回答
0
投票
在遇到问题时删除了h5标签,并以html格式获取其父级的内容,然后剥离结果。

PHP代码更改为

$html = file_get_html(url ); $content = $html->find( 'div[class=faq-d-item]',0) $questions = $content->find( 'div[class=faq-d-item-content easeParent] h5' ); foreach($questions as $k => $question){ $question_array[] = $question->innertext; } $questions1 = $content->find( 'div[class=faq-d-item-content easeParent] h5' ); foreach($questions1 as $qs) { $qs->innertext = ''; } $answers = $content->find( 'div[class=faq-d-item-content easeParent]' ); foreach ( $answers as $ky => $answer ) { if(trim($answer->innertext) == '&nbsp') { $answer->outertext = ''; } $stripped = trim(preg_replace('/\s+/', ' ', strip_tags($answer))); $answer_array[] =$stripped ;

© www.soinside.com 2019 - 2024. All rights reserved.