The Little Guy Posted January 13, 2010 Share Posted January 13, 2010 I have Items in a database with items that look like this: This is a paragraph it isn't apart of the lists below. * list one item. * list one item. * list one item. * list one item. * list one item. * list two item. * list two item. * list two item. * list two item. This is another paragraph it isn't apart of the lists above. I would like to convert it to an un-ordered list. The list items will always be formatted: beginning of line asterisk space string (newline or end of line) as you can see there is a gap between the two lists, and I am not sure of the best way to do this... so basically the final result should look like this: <ul> <li>list one item.</li> <li>list one item.</li> <li>list one item.</li> <li>list one item.</li> </ul> <ul> <li>list two item.</li> <li>list two item.</li> <li>list two item.</li> <li>list two item.</li> </ul> Quote Link to comment Share on other sites More sharing options...
dgoosens Posted January 13, 2010 Share Posted January 13, 2010 hi, my first thought was a explode() the whole string based on the \n after that you loop through the items. If the string starts (substr()) with "* ", you replace it with <li> and close the tag after the string. finally, you'd have to build in a system to add the <ul> and </ul> tags. best thing would be to add the <ul> when the loop encounters the first "* ", add a closing and new opening tag when the loop encounters a blank and close the tag at the end. maybe there are smarter ways to get this done... Quote Link to comment Share on other sites More sharing options...
mattal999 Posted January 13, 2010 Share Posted January 13, 2010 <?php $string = "This is a paragraph it isn't apart of the lists below. * list one item. * list one item. * list one item. * list one item. * list one item. * list two item. * list two item. * list two item. * list two item. This is another paragraph it isn't apart of the lists above."; function makeList($string) { $string = explode("<br />", nl2br($string)); // Could probably just use \n but I wasn't sure. $last = false; $list = ""; foreach($string as $line) { $line = trim($line); if(substr($line, 0, 2) == "* ") { if($last == "ul") { $list .= "<li>".substr($line, 2)."</li>\n"; } else { $list .= "<ul>\n"; $list .= "<li>".substr($line, 2)."</li>\n"; $last = "ul"; } } if(trim($line) == "") { $list .= "</ul>\n"; $last = false; } } if(trim(substr($list, -6)) != "</ul>") $list .= "</ul>"; return $list; } echo makeList($string); ?> Wrote that just for you Wanted a bit of a challenge. Quote Link to comment Share on other sites More sharing options...
dgoosens Posted January 13, 2010 Share Posted January 13, 2010 lol well Little Guy... there you have it... the idea AND the code... nice one mattal999 ! Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted January 13, 2010 Author Share Posted January 13, 2010 <?php $string = "This is a paragraph it isn't apart of the lists below. * list one item. * list one item. * list one item. * list one item. * list one item. * list two item. * list two item. * list two item. * list two item. This is another paragraph it isn't apart of the lists above."; function makeList($string) { $string = explode("<br />", nl2br($string)); // Could probably just use \n but I wasn't sure. $last = false; $list = ""; foreach($string as $line) { $line = trim($line); if(substr($line, 0, 2) == "* ") { if($last == "ul") { $list .= "<li>".substr($line, 2)."</li>\n"; } else { $list .= "<ul>\n"; $list .= "<li>".substr($line, 2)."</li>\n"; $last = "ul"; } } if(trim($line) == "") { $list .= "</ul>\n"; $last = false; } } if(trim(substr($list, -6)) != "</ul>") $list .= "</ul>"; return $list; } echo makeList($string); ?> Wrote that just for you Wanted a bit of a challenge. Awesome! thanks! I did have to add two lines though, so it would work with what I already had: function formatPage($string){ $string = explode("\n", $string); $last = false; $list = ""; foreach($string as $line){ $line = trim($line); if(substr($line, 0, 2) == "* "){ if($last == "ul"){ $list .= "<li>".substr($line, 2)."</li>\n"; }else{ $list .= "<ul>\n"; $list .= "<li>".substr($line, 2)."</li>\n"; $last = "ul"; } }else{// begin addition $list .= '<p>'.$line.'</p>'; }// end addition if(trim($line) == ""){ $list .= "</ul>\n"; $last = false; } } if(trim(substr($list, -6)) != "</ul>") $list .= "</ul>"; return $list; } 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.