1

I have a function called listelements(), which outputs text like <li>text1</li><li>text2</li>.

I have more than 500 elements. Now I want to convert it into an array.

Can anyone help me? Thanks.

Note: I'm using php

Update:

The thing i want to achieve is alphabetical navigation. As of now my function displays links in list order. Instead of that i want to hold that in an array. Then i would like to filter them using characters.

$valid_characters = range( 'a' , 'z' );
$valid_numbers = array(1,2,3,4,5,6,7,8,9,0);

When the user click "A" i would like to display only links start with A. Hope this explanation helps you guys for better understanding my question

Giri
  • 4,849
  • 11
  • 39
  • 48
  • read this : http://php.net/manual/en/class.domelement.php – xkeshav Feb 11 '12 at 11:28
  • wat is the output array you are expecting? – Abhinav Singh Feb 11 '12 at 11:28
  • Do you want only text or with the `li` tag? – xdazz Feb 11 '12 at 11:32
  • 4
    wouldn't it be easier to just modify the function to output an array in the first place instead of the html code? – Sirko Feb 11 '12 at 11:32
  • A comment to my answer states the OP wants `listelemets()` **converted** to return an array, and that some list items contain links. OP please post 1) The source for `listelements()` 2) Details of any/all parameters passed to `listelements()` and 3) The array structure you desire. – Leigh Feb 11 '12 at 11:54
  • @Sirko Yes i think so. But it will be confuse me more. I just a php beginner – Giri Feb 11 '12 at 12:01
  • @user1091558: Still missing the input to the function, and the array structure you expect returned. – Leigh Feb 11 '12 at 12:03
  • @Leigh Sorry, My net got disconnected. Thanks for everything. – Giri Feb 11 '12 at 13:53
  • What is the input into `listelemets()`? Use that, it is likely an array itself. – dotancohen Feb 11 '12 at 11:32

5 Answers5

3
<?php
$output = listelements();
$array = explode("<li>", $output);

//First element will be empty, so remove it
unset($array[0]);

// Now remove "</li>" at end of input
array_walk($array, create_function('&$val', '$val = str_replace("</li>", "", $val)'));

// $array should now contain your elements
iblue
  • 29,609
  • 19
  • 89
  • 128