extract from a string

Thread Solved

Join Date: Sep 2007
Posts: 48
Reputation: verbob is an unknown quantity at this point 
Solved Threads: 1
verbob verbob is offline Offline
Light Poster

extract from a string

 
0
  #1
28 Days Ago
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:

  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; 28 Days Ago at 5:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 48
Reputation: verbob is an unknown quantity at this point 
Solved Threads: 1
verbob verbob is offline Offline
Light Poster
 
0
  #2
28 Days Ago
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; 28 Days Ago at 6:56 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 524
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro
 
0
  #3
28 Days Ago
An explode would be the quickest/easiest way to do this.
  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. }
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 48
Reputation: verbob is an unknown quantity at this point 
Solved Threads: 1
verbob verbob is offline Offline
Light Poster
 
0
  #4
28 Days Ago
Thanks. I am going to get it a shot. Let you know how I make out :-)
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 48
Reputation: verbob is an unknown quantity at this point 
Solved Threads: 1
verbob verbob is offline Offline
Light Poster
 
0
  #5
28 Days Ago
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 48
Reputation: verbob is an unknown quantity at this point 
Solved Threads: 1
verbob verbob is offline Offline
Light Poster
 
0
  #6
28 Days Ago
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; 28 Days Ago at 8:47 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 524
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro
 
0
  #7
28 Days Ago
Use array_pop to remove the last index from the array:
  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; 28 Days Ago at 8:50 pm.
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 48
Reputation: verbob is an unknown quantity at this point 
Solved Threads: 1
verbob verbob is offline Offline
Light Poster
 
0
  #8
28 Days Ago
Don't seem to have that issue anymore once I changed to "unset".

Thanks for the suggestion though
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC