Loop through variable and separate into unique variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
defroster
Forum Commoner
Posts: 49
Joined: Wed Mar 24, 2010 12:05 pm

Loop through variable and separate into unique variables

Post by defroster »

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
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Loop through variable and separate into unique variables

Post by shawngoldw »

Code: Select all

$arr = explode(' ', $tags);
This will result in:

Code: Select all

$arr[0] = "baseball"
$arr[1] = "glove" 
etc

Shawn
defroster
Forum Commoner
Posts: 49
Joined: Wed Mar 24, 2010 12:05 pm

Re: Loop through variable and separate into unique variables

Post by defroster »

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

Post by shawngoldw »

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
defroster
Forum Commoner
Posts: 49
Joined: Wed Mar 24, 2010 12:05 pm

Re: Loop through variable and separate into unique variables

Post by defroster »

Thanks for help, no I would like to loop as many times as there are words.
User avatar
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

Post by AbraCadaver »

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.
defroster
Forum Commoner
Posts: 49
Joined: Wed Mar 24, 2010 12:05 pm

Re: Loop through variable and separate into unique variables

Post by defroster »

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> ";
}
User avatar
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

Post by AbraCadaver »

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.
defroster
Forum Commoner
Posts: 49
Joined: Wed Mar 24, 2010 12:05 pm

Re: Loop through variable and separate into unique variables

Post by defroster »

Thanks, great stuff :)
Post Reply