conker87 Posted June 26, 2007 Share Posted June 26, 2007 As the title suggests. I want to explode a string: $string = "one, two, three" As an example, into something like this: <ul><li>one</li><li>two</li><li>three</li></ul> I've looked around google, but I know you guys are pretty good. Any ideas? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 26, 2007 Share Posted June 26, 2007 Try this: <?php $string = "one, two, three"; $string = explode(",", $string); echo '<ul>'; foreach ($string as $key => $val){ echo '<li>'.trim($val).'</li>'; } echo '</ul>'; ?> EDIT: Cleaned it up a bit. Quote Link to comment Share on other sites More sharing options...
per1os Posted June 26, 2007 Share Posted June 26, 2007 <?php function explode_li($string) { $array = explode("</li>", $string); foreach ($array as $key => $val) { $array[$key] = strip_tags($val); } array_pop($array); // last element should be nothing. return $array } ?> Give that a try. www.php.net/strip_tags www.php.net/explode EDIT: Just noticed what the question was, a different version from poco is <?php $string = "one, two, three"; function explode_tolist($string) { $array = explode(",", $string); $string = "<ul>"; foreach ($array as $val){ $string .= "<li>" . trim($val) . "</li>"; } $string = "</ul>"; } $exploded = explode_tolist($string); echo $exploded; ?> Quote Link to comment Share on other sites More sharing options...
corillo181 Posted June 26, 2007 Share Posted June 26, 2007 $string= array(1,2,3,4,5); foreach($string as $value){ echo '<li>'.$value.'</li>'; } Quote Link to comment Share on other sites More sharing options...
conker87 Posted June 26, 2007 Author Share Posted June 26, 2007 Thanks everyone that replied. Works like a charm. EDIT: Noticed that frost's version is a function. Gunna add that to my functions page. Thanks again ^^ Quote Link to comment Share on other sites More sharing options...
corillo181 Posted June 26, 2007 Share Posted June 26, 2007 ... 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.