jaay Posted December 22, 2009 Share Posted December 22, 2009 Hi, I am new to php and stuck with a problem, need some help I have a field in mysql table that stores tags separated by comma. Now I am using the explode function to separate the tags and display them separtely ... it works fine if theres a space after the comma .... that is like, tag1, tag2, tag3 etc... but if the user doesnt leave a space after the comma ..it doesnt work .. say for example, tag1, tag2,tag3 ..in this case only tag1 and tag2 gets displayed not tag3 .... what am i doing wrong here? here's my code .. $stags = explode(", ", $a); $result = count($stags); for($n = 0; $n < $result; $n++) { $stags = explode (",", $a); echo trim($stags [$n]); } Link to comment https://forums.phpfreaks.com/topic/186018-help-with-explode/ Share on other sites More sharing options...
Buddski Posted December 22, 2009 Share Posted December 22, 2009 Try this: <?php $tags = "tag1, tag2,tag3"; $etags = explode(',',$tags); foreach ($etags as $tag) { echo trim($tag); } Link to comment https://forums.phpfreaks.com/topic/186018-help-with-explode/#findComment-982272 Share on other sites More sharing options...
teynon Posted December 22, 2009 Share Posted December 22, 2009 $stags = explode(", ", $a); has a space in it. Trim removes additional spacing. Just make it $stags = explode(",", $a); Link to comment https://forums.phpfreaks.com/topic/186018-help-with-explode/#findComment-982275 Share on other sites More sharing options...
cags Posted December 22, 2009 Share Posted December 22, 2009 Alternative to buddski's code. $tags = "tag1, tag2,tag3"; $etags = explode(',',$tags); $etags = array_map('trim', $etags); Link to comment https://forums.phpfreaks.com/topic/186018-help-with-explode/#findComment-982284 Share on other sites More sharing options...
jaay Posted December 22, 2009 Author Share Posted December 22, 2009 $stags = explode(", ", $a); has a space in it. Trim removes additional spacing. Just make it $stags = explode(",", $a); It worked! Thanks everyone for the help. Link to comment https://forums.phpfreaks.com/topic/186018-help-with-explode/#findComment-982335 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.