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:

<li>item1</li><li>item2</li><li>item3</li>

(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?

Recommended Answers

All 7 Replies

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.

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?

SUCCESS!!

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.

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

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

Thanks for the suggestion though

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.