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

9
  • read this : php.net/manual/en/class.domelement.php Commented Feb 11, 2012 at 11:28
  • wat is the output array you are expecting? Commented Feb 11, 2012 at 11:28
  • What is the input into listelemets()? Use that, it is likely an array itself. Commented Feb 11, 2012 at 11:32
  • Do you want only text or with the li tag? Commented Feb 11, 2012 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? Commented Feb 11, 2012 at 11:32

5 Answers 5

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
Sign up to request clarification or add additional context in comments.

2 Comments

In case there is something between 2 li-s, this does not work fine.
Sure, but OP specified the format as multiple concatations of "<li>$element</li>", so no problem there.
2

explode won't do the trick nicely for html tags (considering them as multiple delimiters).

if CPU time is not a concern, try using preg_match, example below:

<?PHP

$input='<li>text1</li><li>text2</li><LI><p>text3</p></lI><Li>text fou4r</li>';

preg_match_all('(<(li|Li|LI|lI)>(.*)</(li|Li|LI|lI)>)siU', $input, $output);

print_r($output[2]);
?>


output:

Array

    (

        [0] => text1
        [1] => text2
        [2] => <p>text3</p>
        [3] => text fou4r
    )

3 Comments

If your pattern is case insensitive, why are you matching all variations of case? And even then why would you do that instead of [LlIi]?
I think its better solution than iblue's above... Use regexp instead of explode and str_replace.
0
$strarr=explode("<li>",$string); //Breaks every <li>
$i=(-1); //Makes -1 as array starts at 0
$arr=array(); //this will be your array
foreach($strarr as $expl){ //Go through all li's
$ai=explode("</li>",$expl);//Get between the li. If there is something between </li> and <li>, it won't return it as you don't need it
if($i!=(-1))$arr[$i]=$ai[0]; //First isn't valid
$i=$i+1; //add i plus one to make it a real array
}

2 Comments

This has syntax errors; explode() needs an assignment, and there's no "new" keywords for arrays
Sorry, my bad, I always forget that. Corrected.
0

you can use simple_html_dom https://simplehtmldom.sourceforge.io/manual.htm

require_once(./simple_html_dom.php);
$html = "<ul class=''><li class=''>Swagger transforms unfreshmen into legends of confidence</li><li class=''>Red Zone collection</li><li class=''>Enhances awesomeness</li><li class=''>Continue your confidence with Swagger Anti-perspirant & Deodorant and Body Spray</li><li class=''>Old Spice Red Zone Swagger Scent Men’s Bar Soap</li></ul>";

$result = li_to_array($html);
var_dump($result ); 

    
function li_to_array($str)
{
    if (is_string($str) && !empty($str)) {
        $html = str_get_html($str);
        $results= array();
        $response_results = $html->find('li');
        foreach ($response_results as $response_result){
            array_push($results, $response_result->plaintext);
        } 
        unset($html); //release memory

        return  $results;
    } else {
        return false;
    }
}

Comments

-1

Use "preg_match".

Here is more detail: http://www.php.net/manual/en/function.preg-match-all.php

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.