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? Link to comment https://forums.phpfreaks.com/topic/57328-solved-explode-to/ 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. Link to comment https://forums.phpfreaks.com/topic/57328-solved-explode-to/#findComment-283421 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; ?> Link to comment https://forums.phpfreaks.com/topic/57328-solved-explode-to/#findComment-283422 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>'; } Link to comment https://forums.phpfreaks.com/topic/57328-solved-explode-to/#findComment-283424 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 ^^ Link to comment https://forums.phpfreaks.com/topic/57328-solved-explode-to/#findComment-283438 Share on other sites More sharing options...
corillo181 Posted June 26, 2007 Share Posted June 26, 2007 ... Link to comment https://forums.phpfreaks.com/topic/57328-solved-explode-to/#findComment-283439 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.