我有以下代码,它检索第一个pargraph并回应它:
function first_paragraph() {
global $post, $posts;
$first_para = '';
ob_start();
ob_end_clean();
$post_content = $post->post_content;
$post_content = apply_filters('the_content', $post_content);
$output = preg_match_all('%(<p[^>]*>.*?</p>)%i', $post_content, $matches);
$first_para = $matches [1] [0];
echo $first_para;
}
但是,我想创建第二个函数,它会获得所有段落。有谁知道如何修改第一个代码,所以它检索所有段落?
答案 0 :(得分:0)
为什么不使用DOMDocument
Class?
<?php
$html='<p>First Paragraph</p>
<a href="somesite">click</a>
<p>Second ....</p>
<p>Third .. Phew last one!</p>';
$dom = new DOMDocument;
@$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('p') as $tag) {
echo $tag->nodeValue."<br>"; // echoes all paragraph one by one
}
输出:
First Paragraph
Second ....
Third .. Phew last one!