<li> 从帖子内容中提取第一个标签条目?



我想为教育网站创建一个自定义摘录。它有包含<ul><ol>列表的帖子。我想打印帖子中ulol标签的第一个子标签。目前,我有以下代码,只能打印第一段。

function wpden_excerpt()
{
global $post;
$output = get_the_content();
$wanted_number_of_paragraph = 1;
$tmp = explode ('</li>', $output);
for ($i = 0; $i < $wanted_number_of_paragraph; ++$i) {
if (isset($tmp[$i]) && $tmp[$i] != '') {
$tmp_to_add[$i] = $tmp[$i];
}
}
$output = implode('</li>', $tmp_to_add) . '</li>';
echo $output;
}

HTML内容:

<p>Once in a blue moon</p>
<p>Meaning</p>
<ul>
<li>not very often</li>
<li>rarely</li>
<li>once after a long time</li>
</ul>
<p>Examples</p>
<ol>
<li>My sister lives in Alaska, so I only see her once in a blue moon.</li>
<li>Once in a blue moon, there's an issue I can't resolve.</li>
<li>That company puts on a good performance only once in a blue moon.</li>
</ol>

使用:打印自定义摘录

<?php wpden_excerpt(); ?>

现在我想从HTML内容中打印出类似的内容:

<p>
<b>Meaning:</b> not very often<br /> <!-- Content from first <li> of first <ul> -->
<b>Example:</b> My sister lives in Alaska, so I only see her once in a blue moon. <!-- Content from first <li> of first <ol> -->
</p>

试试这个:

<?php
$post = "<p>Once in a blue moon</p>
<p>Meaning</p>
<ul>
<li>not very often</li>
<li>rarely</li>
<li>once after a long time</li>
</ul>
<p>Examples</p>
<ol>
<li>My sister lives in Alaska, so I only see her once in a blue moon.</li>
<li>Once in a blue moon, there's an issue I can't resolve.</li>
<li>That company puts on a good performance only once in a blue moon.</li>
</ol>";
$arr1 = explode("</li>",explode("<ul>", $post)[1]);
$arr2 = explode("</li>",explode("<ol>",$post)[1]); 

echo "<p>
<b>Meaning:</b>".substr($arr1[0],4)."<br /><b>Example:</b>".substr($arr2[0],4)."</p>";
?>

此方法适用于您吗?

<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var _ul_f = $('ul li:first').text();
var _ol_f = $('ol li:first').text();
$('#output').html('<p><b>Meaning:</b>'+_ul_f+'</br><b>Example:</b>'+_ol_f+'</p>');
});
</script>
<div id="output">
</div>

您可以使用DOMDocument加载HTML,然后获取每个块的子级。使用CCD_ 3和CCD_。通过将HTML放在变量中,可以分别解析每个变量。只需在页面上回显变量即可显示其内容。

$ulcontent = "
<p>Once in a blue moon</p>
<p>Meaning</p>
<ul>
<li>not very often</li>
<li>rarely</li>
<li>once after a long time</li>
</ul>
";
$olcontent = "
<p>Examples</p>
<ol>
<li>My sister lives in Alaska, so I only see her once in a blue moon.</li>
<li>Once in a blue moon, there's an issue I can't resolve.</li>
<li>That company puts on a good performance only once in a blue moon.</li>
</ol>
";
$uldom = new DOMDocument;
$uldom->loadHTML($ulcontent);
$oldom = new DOMDocument;
$oldom->loadHTML($olcontent);
$firstChildOfUl = $uldom->getElementsByTagName('li')->item(0)->childNodes->item(0);
$ulvalue = $firstChildOfUl->nodeValue;
$firstChildOfOl = $oldom->getElementsByTagName('li')->item(0)->childNodes->item(0);
$olvalue = $firstChildOfOl->nodeValue;
echo "<PRE>",var_dump($ulvalue),"</PRE>";
echo "<PRE>",var_dump($olvalue),"</PRE>";

输出:

UL的第一个子级:string(14) "not very often"OL的第一个子级:string(65) "My sister lives in Alaska, so I only see her once in a blue moon."

PHP页面上的HTML:

<?=$ulcontent?>
<?=$olcontent?>

HTML输出:<?=$ulcontent?>&<?=$olcontent?>

千载难逢的

意思是

不经常很少地很久以后一次示例

我姐姐住在阿拉斯加,所以我难得一见她。千载难逢,有一个问题我无法解决。那家公司业绩好是千载难逢的。

最新更新