I would like to take a string (I have loaded it from the database) and extract it into chunks.

The beginning string, lets call it "$string", has the following format:


(maybe more li's, maybe less, or even none)

What I would like to do is extract to an array each li, display each array element seperately ( with an option to "delete it"), then after add them back to the string and place back in the database. And yes I want each array element to include the li tags within(not that its necessary, I guess I could get around it)

I understand most of it, just getting hung up on how to extract each from the string and place into an array.

Anyone have an ideas how to go about this?

Edited by verbob: n/a

8 Years
Discussion Span
Last Post by verbob

is it possible for the seperator of the explode function to be more than one character long? (I had a thought of using explode to extract the individual elements using "</li>" as the seperator)...then I can just readd it later.

Sounds logical anyway.

Edited by verbob: n/a


An explode would be the quickest/easiest way to do this.

$liArray = explode("</li>", $string);

foreach($liArray as $key => $value) {
  // $value will contain the li string, do something with it here. 

Thanks. I am going to get it a shot. Let you know how I make out :-)


Thank you, seems to work ok.

Although there seems to be a slight "issue" with there being a "seperator" at the END of the string.

Any way around that?



My script was being used to delete items, so instead of setting the item ="" (which left a trailing "</li>" when I imploded back), I used unset to remove the key from the array.

Thanks Will.

Edited by verbob: n/a


Use array_pop to remove the last index from the array:

$liArray = explode("</li>", $string);
// $liArray_pop will contain the last index of $liArray, $liArray will be missing the final index
$liArray_pop = array_pop($liArray);

Edited by Will Gresham: n/a


Don't seem to have that issue anymore once I changed to "unset".

Thanks for the suggestion though

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.