Connecting Tech Pros Worldwide Help | Site Map

PHP String Thing

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 11:29 AM
Trevor
Guest
 
Posts: n/a
Default PHP String Thing

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


  #2  
Old July 17th, 2005, 11:29 AM
Duyet The Vo
Guest
 
Posts: n/a
Default 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]


  #3  
Old July 17th, 2005, 11:29 AM
Dave Patton
Guest
 
Posts: n/a
Default Re: PHP String Thing

"Trevor" <trevordixon@gmail.com> wrote in
news:1108280762.365123.210860@l41g2000cwc.googlegr oups.com:
[color=blue]
> Say I've got this:
>
><?php
>
> $string = "Bill,Martin,Joe";
> $name1
> $name2
> $name3
>
> ?>[/color]

OK, we'll say that you have a PHP script that has one line
of code followed by three lines with parse errros ;-)
[color=blue]
> How do I make the $name1 equal to the first name in the list of
> strings, $name2 the second and so on?[/color]

http://www.php.net/manual/en/ref.strings.php
http://www.php.net/manual/en/function.explode.php

--
Dave Patton
Canadian Coordinator, Degree Confluence Project
http://www.confluence.org/
My website: http://members.shaw.ca/davepatton/
  #4  
Old July 17th, 2005, 11:29 AM
dourdoun
Guest
 
Posts: n/a
Default 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]
  #5  
Old July 17th, 2005, 11:29 AM
Janwillem Borleffs
Guest
 
Posts: n/a
Default 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



  #6  
Old July 17th, 2005, 11:29 AM
Trevor
Guest
 
Posts: n/a
Default Re: PHP String Thing

Thank you very much, that's what I needed.

Trevor

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

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 220,989 network members.