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!! Link to comment https://forums.phpfreaks.com/topic/81973-solved-string-manipulation/ 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! Link to comment https://forums.phpfreaks.com/topic/81973-solved-string-manipulation/#findComment-416717 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! Link to comment https://forums.phpfreaks.com/topic/81973-solved-string-manipulation/#findComment-416719 Share on other sites More sharing options...
mfindlay Posted December 17, 2007 Author Share Posted December 17, 2007 sorry to be a nuisance... bump!! Link to comment https://forums.phpfreaks.com/topic/81973-solved-string-manipulation/#findComment-416786 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 Link to comment https://forums.phpfreaks.com/topic/81973-solved-string-manipulation/#findComment-416803 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 Link to comment https://forums.phpfreaks.com/topic/81973-solved-string-manipulation/#findComment-416813 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 Link to comment https://forums.phpfreaks.com/topic/81973-solved-string-manipulation/#findComment-417082 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.