defroster Posted August 26, 2010 Share Posted August 26, 2010 Hello, If I have this string: $tags="baseball glove face" (this can vary from 1 to 15 different words) how would i loop through and divide these into different $tag1=baseball $tag2=glove $tag3=face .. and so on (if more words) Thanks for help Link to comment https://forums.phpfreaks.com/topic/211798-loop-through-variable-and-separate-items-into-unique-variables/ Share on other sites More sharing options...
AbraCadaver Posted August 26, 2010 Share Posted August 26, 2010 Better using an array: $tags = "baseball glove face"; $tag = str_word_count($tags, 1); Link to comment https://forums.phpfreaks.com/topic/211798-loop-through-variable-and-separate-items-into-unique-variables/#findComment-1104006 Share on other sites More sharing options...
defroster Posted August 26, 2010 Author Share Posted August 26, 2010 Thanks I figured it out with your help. $tags=$row['tags']; $arraytags = str_word_count($tags, 1); while (list(, $value) = each($arraytags)) { echo "<a href='search.php?q=".$value."'>".$value."</a> "; } Link to comment https://forums.phpfreaks.com/topic/211798-loop-through-variable-and-separate-items-into-unique-variables/#findComment-1104030 Share on other sites More sharing options...
AbraCadaver Posted August 26, 2010 Share Posted August 26, 2010 Didn't know you wanted to know how to loop. This is shorter: $tags = $row['tags']; foreach(str_word_count($tags, 1) as $value) { echo '<a href="search.php?q="'.$value.'">'.$value.'</a>'; } Link to comment https://forums.phpfreaks.com/topic/211798-loop-through-variable-and-separate-items-into-unique-variables/#findComment-1104037 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.