mfindlay Posted December 17, 2007 Share Posted December 17, 2007 Hi, In the code below I am trying to format an unordered list with php, I am using JavaScript to add tags to a textarea and then want to format the list in the php script...so: <ul><li>List 1<br>List 2<br>List 3</li></ul> //would become <ul> <li>list 1</li> <li>list 2</li> ... </ul> I have been using lots of str_replace to strip esscape chars etc, which is why the code is so far formatted as above, below is what I have so far.. $add_data = "\"" . $explanation . "\"" . "," . "\"" . $question_title . "\"" . "," . "\"" . $question_stem . "\"\n"; $add_data = str_replace("\n" , "<BR>" , $add_data); $add_data = str_replace("\r" , "" , $add_data); $add_data = str_replace("<UL>" , "<UL><LI>" , $add_data); $add_data = str_replace("<\UL>" , "<\LI><\UL>" , $add_data); $add_data = str_replace("\"<BR>" , "\"\n" , $add_data); any help apreciated.... Cheers!! Quote Link to comment Share on other sites More sharing options...
mfindlay Posted December 17, 2007 Author Share Posted December 17, 2007 Hello again... with regard to above, I suppose I want to find the start (<ul><li>) and end (</li></ul>) of the portion of the string and replace all the occurrences of <br> with </li><li> (this part is easy), I'm just not sure how to find the start and end points?? again any help appreciated.... Cheers! Quote Link to comment Share on other sites More sharing options...
mfindlay Posted December 17, 2007 Author Share Posted December 17, 2007 sorry some code was emmitted.... with regard to above, I suppose I want to find the start (<ul><li>) and end (</li></ul>) of the portion of the string and replace all the occurrences of <br> with </li><li> (this part is easy), I'm just not sure how to find the start and end points?? again any help appreciated.... Cheers! Quote Link to comment Share on other sites More sharing options...
mfindlay Posted December 17, 2007 Author Share Posted December 17, 2007 sorry to be a nuisance... bump!! Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 17, 2007 Share Posted December 17, 2007 Here's one method: <?php $str = '<ul><li>List 1<br>List 2<br>List 3</li></ul>'; $new_str = implode("</li>\n<li>",explode("<br>",$str)); $new_str = nl2br(str_replace(array("<ul>","</ul>"),array("<ul>\n","\n</ul>"),$new_str)) . "\n"; echo $new_str; ?> Ken Quote Link to comment Share on other sites More sharing options...
mfindlay Posted December 17, 2007 Author Share Posted December 17, 2007 Thanks Ken, the list is just part of a string, so I guess the actual string could look more like... <b>some text</b> <br><br> <ul><li>List 1<br>List 2<br>List 3</li></ul> some more text... so I need to search for the start and end of the list and just change the breaks in the middle Thanks Quote Link to comment Share on other sites More sharing options...
mfindlay Posted December 17, 2007 Author Share Posted December 17, 2007 figured it out!! this is what I came up with... for ($ul = 0; $ul < substr_count($add_data , "<UL><LI>"); $ul++) { $find_ul_start = '<UL><LI>'; $find_ul_end = '</LI></UL>'; $ul_start = strpos($add_data, $find_ul_start); $ul_end = strpos($add_data, $find_ul_end); $length = $ul_end - $ul_start; } $new_string = substr($add_data,$ul_start,$length); $new_string1 = str_replace("<BR>" , "</LI><LI>" , $new_string); $add_data = str_replace($new_string , $new_string1 , $add_data); Cheers Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.