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

12,893

Solution 1

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);

Solution 2

You will need to parse the HTML code get the text out. DOM parser can be used for this purpose.

   $DOM = new DOMDocument;
   $DOM->loadHTML($str); // $str is your HTML code as a string

   //get all H3 
   $items = $DOM->getElementsByTagName('h3');

Solution 3

It might be easier to parse it in Javascript (perhaps using jQuery), and then send it to your PHP with some AJAX.

// Javascript/jQuery
var array = [];
$("h3").each(function() {
    array.push($(this).html());
});

var message = JSON.stringify(array);
$.post('test.php', {data: message}, function(data) {
    document.write(data); // "success"
}

Then in PHP:

<?php

$data = $_POST['data'];

// convert json into array
$array = json_decode($data);

// do stuff with your data
// then send back whatever you need

echo "success";

?>
Share:
12,893
RoseCoder
Author by

RoseCoder

Updated on June 05, 2022

Comments

  • RoseCoder
    RoseCoder almost 2 years

    Im trying to store the string value's of a list item on my website into a variable/array in PHP to do some conditional checks/statements with them. Am having a bit off 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".

  • Manoj Yadav
    Manoj Yadav almost 11 years
    Its working fine for me, online output link codepad.org/VyLj8cU0
  • RoseCoder
    RoseCoder almost 11 years
    This works great, is there anyway of dynamically retrieving whats stored int he $html variable without declaring it like that? Many thanks
  • Manoj Yadav
    Manoj Yadav almost 11 years
    Yes pass the dynamic string to loadHTML like $doc->loadHTML($dynamic_string);
  • Ali
    Ali about 10 years
    @ManojYadav one more thing, what if i also want to get the href attribute of every <li> tag? Please help.
  • Manoj Yadav
    Manoj Yadav about 10 years
    @Ali use DOMElement::getAttribute function to get a attribute value. in3.php.net/manual/en/domelement.getattribute.php and I think you mean how to get href of a tag not li tag
  • Ali
    Ali about 10 years
    Yes Manoj, i meant to say that i have a ul of almost 65 li and every li has a link upon it. So what i want to do is to get only first 5 li in an array (along with the associated link) and print them along with the href associated with their respective li. Hope you get it :)
  • Manoj Yadav
    Manoj Yadav about 10 years
    To get href of a in li try this $li->getElementsByTagName('a')->item(0)->getAttribute('href'‌​) or if href in li then try this $li->getAttribute('href')