php - Extract first <li> tag entries from a post content?
Get the solution ↓↓↓I want to create a custom excerpt for an education website. It has posts that contains<ul>
and<ol>
listing. I want to print the first child of both ul and ol tags from the post.
Currently, I have the following code that can print only the first paragraph.
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 Content:
<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>
Printing custom excerpt with:
<?php wpden_excerpt(); ?>
Now I want to print something like that out of the HTML Content:
<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>
Answer
Solution:
try this :
<?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>";
?>
Answer
Solution:
Does this method work for you?
<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>
Answer
Solution:
You can use DOMDocument load the HTML and then get the children of each block. usingchildNodes
andnodeValues
. By placing your HTML in variables, this allows you to parse each variable separately. Simply echo out your variable on the page to display its content.
$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>";
OUTPUT:
First Child of UL:string(14) "not very often"
First Child of OL:string(65) "My sister lives in Alaska, so I only see her once in a blue moon."
HTML on PHP page:
<?=$ulcontent?>
<?=$olcontent?>
HTML OUTPUT of :<?=$ulcontent?>
&<?=$olcontent?>
Once in a blue moon
Meaning
not very often rarely once after a long time Examples
My sister lives in Alaska, so I only see her once in a blue moon. Once in a blue moon, there's an issue I can't resolve. That company puts on a good performance only once in a blue moon.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: installation failed, reverting ./composer.json and ./composer.lock to their original content
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.