944,045 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 616
  • PHP RSS
Nov 4th, 2009
0

extract from a string

Expand Post »
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:

PHP Syntax (Toggle Plain Text)
  1. <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?
Last edited by verbob; Nov 4th, 2009 at 5:49 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
verbob is offline Offline
62 posts
since Sep 2007
Nov 4th, 2009
0
Re: extract from a string
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.
Last edited by verbob; Nov 4th, 2009 at 6:56 pm.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
verbob is offline Offline
62 posts
since Sep 2007
Nov 4th, 2009
0
Re: extract from a string
An explode would be the quickest/easiest way to do this.
PHP Syntax (Toggle Plain Text)
  1. $liArray = explode("</li>", $string);
  2.  
  3. foreach($liArray as $key => $value) {
  4. // $value will contain the li string, do something with it here.
  5. }
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Nov 4th, 2009
0
Re: extract from a string
Thanks. I am going to get it a shot. Let you know how I make out :-)
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
verbob is offline Offline
62 posts
since Sep 2007
Nov 4th, 2009
0
Re: extract from a string
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?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
verbob is offline Offline
62 posts
since Sep 2007
Nov 4th, 2009
0
Re: extract from a string
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.
Last edited by verbob; Nov 4th, 2009 at 8:47 pm.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
verbob is offline Offline
62 posts
since Sep 2007
Nov 4th, 2009
0
Re: extract from a string
Use array_pop to remove the last index from the array:
PHP Syntax (Toggle Plain Text)
  1. $liArray = explode("</li>", $string);
  2. // $liArray_pop will contain the last index of $liArray, $liArray will be missing the final index
  3. $liArray_pop = array_pop($liArray);
Last edited by Will Gresham; Nov 4th, 2009 at 8:50 pm.
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Nov 4th, 2009
0
Re: extract from a string
Don't seem to have that issue anymore once I changed to "unset".

Thanks for the suggestion though
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
verbob is offline Offline
62 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Question about mass mail function
Next Thread in PHP Forum Timeline: PHP to display content in editable text area





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC