[使用php解析器获取带有类的p标记中的所有子项
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[使用php解析器获取带有类的p标记中的所有子项相关的知识,希望对你有一定的参考价值。
我正在使用简单的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;"> · balance in such accounts at any point of time should not exceed Rs.50,000</p> <p style="margin-left:1.0cm;"> · total credits in one year should not exceed Rs.1,00,000</p> <p style="margin-left:1.0cm;"> · total withdrawal and transfers should not exceed Rs.10,000 in a month.</p> <p style="margin-left:1.0cm;"> · 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. </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 = 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) == ' ') {
$answer->outertext = '';
}
$stripped = trim(preg_replace('/s+/', ' ', strip_tags($answer)));
$answer_array[] =$stripped ;
以上是关于[使用php解析器获取带有类的p标记中的所有子项的主要内容,如果未能解决你的问题,请参考以下文章
PHP - BBCode 解析器 - 解析 bbcode 链接标记和未标记链接