Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ul li a string values and store them in a variable or array php [duplicate]

I'm trying to store the string values of a list item on my website into a variable/array in PHP to do some conditional checks/statements with them. I am having a bit of difficulty getting the list item's string value using PHP. Can anybody help?

This is the markup.

<div class="coursesListed">
<ul>
<li><a href="#"><h3>Item one</h3></a></li>
<li><a href="#"><h3>item two</h3></a></li>
<li><a href="#"><h3>Item three</h3></a></li>            
</ul>
</div>

What I want ideally is either a variable or array that holds the values "Item one", "Item two", "Item three".

like image 597
RoseCoder Avatar asked Oct 24 '25 15:10

RoseCoder


1 Answers

Try this

$html = '<div class="coursesListed">
<ul>
<li><a href="#"><h3>Item one</h3></a></li>
<li><a href="#"><h3>item two</h3></a></li>
<li><a href="#"><h3>Item three</h3></a></li>            
</ul>
</div>';

$doc = new DOMDocument();
$doc->loadHTML($html);
$liList = $doc->getElementsByTagName('li');
$liValues = array();
foreach ($liList as $li) {
    $liValues[] = $li->nodeValue;
}

var_dump($liValues);
like image 169
Manoj Yadav Avatar answered Oct 27 '25 04:10

Manoj Yadav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!