0

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?

Edited by verbob: n/a

2
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by verbob
0

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

0

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. 
}
0

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

0

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?

0

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.

Edited by verbob: n/a

0

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

0

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.