PHP String Thing 
July 17th, 2005, 12:29 PM
| | | |
Say I've got this:
<?php
$string = "Bill,Martin,Joe";
$name1
$name2
$name3
?>
How do I make the $name1 equal to the first name in the list of
strings, $name2 the second and so on?
Thank You,
Trevor | 
July 17th, 2005, 12:29 PM
| | | | re: PHP String Thing
$string = "Bill,Martin,Joe";
list ($name1,$name2,$name3) = split ( ",", $string );
"Trevor" <trevordixon@gmail.com> wrote in message
news:1108280762.365123.210860@l41g2000cwc.googlegr oups.com...[color=blue]
> Say I've got this:
>
> <?php
>
> $string = "Bill,Martin,Joe";
> $name1
> $name2
> $name3
>
> ?>
>
> How do I make the $name1 equal to the first name in the list of
> strings, $name2 the second and so on?
>
> Thank You,
> Trevor
>[/color] | 
July 17th, 2005, 12:29 PM
| | | | re: PHP String Thing
Simple Answer:
<?php
$string = "Bill,Martin,Joe";
list($name1, $name2, $name3) = explode(",", $string);
?>
Complicated, for any number of varibles:
<?php
$string = "Bill,Martin,Joe";
$names = explode(",", $string);
for($i = 0; $i < count($names); $i++)
{
eval('$name'.($i+1).' = "'.$names[$i].'";');
}
print $name1; // Prints 'Bill'
print $name2; // Prints 'Martin'
print $name3; // Prints 'Joe'
?>
hope it helps,
Vasilis
"Trevor" <trevordixon@gmail.com> wrote in message news:<1108280762.365123.210860@l41g2000cwc.googleg roups.com>...[color=blue]
> Say I've got this:
>
> <?php
>
> $string = "Bill,Martin,Joe";
> $name1
> $name2
> $name3
>
> ?>
>
> How do I make the $name1 equal to the first name in the list of
> strings, $name2 the second and so on?
>
> Thank You,
> Trevor[/color] | 
July 17th, 2005, 12:29 PM
| | | | re: PHP String Thing
dourdoun wrote:[color=blue]
> for($i = 0; $i < count($names); $i++)
> {
> eval('$name'.($i+1).' = "'.$names[$i].'";');
> }
>[/color]
You don't need eval for this:
for ($i = 0; $i < count($names); $i++) {
${'name'.($i+1)} = $names[$i];
}
JW | 
July 17th, 2005, 12:29 PM
| | | | re: PHP String Thing
Thank you very much, that's what I needed.
Trevor |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,702 network members.
|