Loop through variable and separate into unique variables
Moderator: General Moderators
Loop through variable and separate into unique variables
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
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
-
shawngoldw
- Forum Contributor
- Posts: 212
- Joined: Mon Apr 05, 2010 3:38 pm
Re: Loop through variable and separate into unique variables
Code: Select all
$arr = explode(' ', $tags);Code: Select all
$arr[0] = "baseball"
$arr[1] = "glove"
etcShawn
Re: Loop through variable and separate into unique variables
Thanks, that is great. How could I write the code so if there were 12 words(tags) it would loop through only 12 times?
-
shawngoldw
- Forum Contributor
- Posts: 212
- Joined: Mon Apr 05, 2010 3:38 pm
Re: Loop through variable and separate into unique variables
Well it will do it for however many words are in the senetence. If there are 12 it will only make $arr[0], $arr[1], ... $arr[11].
Are you trying to limit it to 12?
Shawn
Are you trying to limit it to 12?
Shawn
Re: Loop through variable and separate into unique variables
Thanks for help, no I would like to loop as many times as there are words.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Loop through variable and separate into unique variables
Alternate:
Code: Select all
$tags = "baseball glove face";
$tag = str_word_count($tags, 1);mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: Loop through variable and separate into unique variables
I figured it out. Thanks for help.
Code: Select all
$tags=$row['tags'];
$arraytags = str_word_count($tags, 1);
while (list(, $value) = each($arraytags)) {
echo "<a href='search.php?q=".$value."'>".$value."</a> ";
}
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Loop through variable and separate into unique variables
Didn't know you wanted to know how to loop. This is shorter:
Code: Select all
$tags = $row['tags'];
foreach(str_word_count($tags, 1) as $value) {
echo '<a href="search.php?q="'.$value.'">'.$value.'</a>';
}mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: Loop through variable and separate into unique variables
Thanks, great stuff 