pacchiee Posted July 3, 2009 Share Posted July 3, 2009 Hello, I wanted to convert a string to an ordered list. Something like below: EX-1 $string = "a,b,c,d,e,f"; The above string should be listed as 1. a 2. b 3. c 4. d 5. e 6. f EX-2 $string = "a,b"; The above string should be listed as 1. a 2. b EX-3 $string = "a"; The output should be 1. a I thought of using list(), but the number of variables may differ in the string and don't know how to handle it. Please help. Thanks in Advance. Link to comment https://forums.phpfreaks.com/topic/164688-solved-converting-string-to-orderd-list/ Share on other sites More sharing options...
.josh Posted July 3, 2009 Share Posted July 3, 2009 explode foreach Link to comment https://forums.phpfreaks.com/topic/164688-solved-converting-string-to-orderd-list/#findComment-868460 Share on other sites More sharing options...
pacchiee Posted July 3, 2009 Author Share Posted July 3, 2009 Can you please give an example how to use explode() and foreach() to get an ordered list as I mentioned above? Link to comment https://forums.phpfreaks.com/topic/164688-solved-converting-string-to-orderd-list/#findComment-868465 Share on other sites More sharing options...
gevans Posted July 3, 2009 Share Posted July 3, 2009 Follow th links, it takes you to the php maual which has plenty of examples. Link to comment https://forums.phpfreaks.com/topic/164688-solved-converting-string-to-orderd-list/#findComment-868466 Share on other sites More sharing options...
sasa Posted July 3, 2009 Share Posted July 3, 2009 try <?php $test = 'a,b,c,d'; echo '<ol><li>', str_replace(',','</li><li>',$test),'</li></ol>'; ?> Link to comment https://forums.phpfreaks.com/topic/164688-solved-converting-string-to-orderd-list/#findComment-868471 Share on other sites More sharing options...
pacchiee Posted July 3, 2009 Author Share Posted July 3, 2009 It worked! Thanks sasa. Link to comment https://forums.phpfreaks.com/topic/164688-solved-converting-string-to-orderd-list/#findComment-868475 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.