473,386 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Jul 17 '05 #1
5 1250
$string = "Bill,Martin,Joe";

list ($name1,$name2,$name3) = split ( ",", $string );

"Trevor" <tr*********@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
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

Jul 17 '05 #2
"Trevor" <tr*********@gmail.com> wrote in
news:11**********************@l41g2000cwc.googlegr oups.com:
Say I've got this:

<?php

$string = "Bill,Martin,Joe";
$name1
$name2
$name3

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


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/
Jul 17 '05 #3
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" <tr*********@gmail.com> wrote in message news:<11**********************@l41g2000cwc.googleg roups.com>...
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

Jul 17 '05 #4
dourdoun wrote:
for($i = 0; $i < count($names); $i++)
{
eval('$name'.($i+1).' = "'.$names[$i].'";');
}


You don't need eval for this:

for ($i = 0; $i < count($names); $i++) {
${'name'.($i+1)} = $names[$i];
}
JW

Jul 17 '05 #5
Thank you very much, that's what I needed.

Trevor

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nick Keighley | last post by:
Hi, I want to store the representation of a class (called Thing in this post) in a string. The class provides no access to Thing's internal components but does provide an operator<<(). Defined...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
4
by: Dennis Myrén | last post by:
Hi. Is there a way to utilize the great primitive data type formatting routines available in .NET without working with strings? I want a byte directly rather than a string. I think it is...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
9
by: Divick | last post by:
Hi all, I have a problem related to std::string class. Is it ok to assign a global string variable to a local string object as shown below? I am trying to print the address of local string...
10
by: klineb | last post by:
Good Day, I have written and utility to convert our DOS COBOL data files to a SQL Server database. Part of the process requires parsing each line into a sql statement and validting the data to...
12
by: Vincent RICHOMME | last post by:
Hi, I am currently implementing some basic classes from .NET into modern C++. And I would like to know if someone would know a non mutable string class.
7
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.