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
mcog_esteban
Forum Contributor
Posts: 127 Joined: Tue Dec 30, 2003 3:28 pm
Post
by mcog_esteban » Tue Aug 28, 2007 8:17 am
Hello.
How can i cast a string like :
Code: Select all
$str = "post[0] = array('Field1', 'Field2', 'Field3');";
to an array ?
I have tryed
and gave me
Parse error: syntax error, unexpected '=' in /opt/lampp/htdocs/interacesso2/consola/ex1.php(5) : eval()'d code on line 1
i know that the string must be ended with a semicolon, but the semicolon is there, or not?
Last edited by
mcog_esteban on Wed Aug 29, 2007 4:39 am, edited 1 time in total.
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Tue Aug 28, 2007 8:22 am
1. Variables in PHP begin with $ - all of them, even array variables
2. Try to avoid usage of eval()
There are 10 types of people in this world, those who understand binary and those who don't
mcog_esteban
Forum Contributor
Posts: 127 Joined: Tue Dec 30, 2003 3:28 pm
Post
by mcog_esteban » Tue Aug 28, 2007 8:27 am
VladSun wrote: 1. Variables in PHP begin with $ - all of them, even array variables
2. Try to avoid usage of eval()
About 1, it was a bad copy and paste
.
What you advice to get the variable
without using
?
Thank you.
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Tue Aug 28, 2007 8:34 am
Well, it is not a simple typo
1. You have to write it in such way that the $post in the string is interpreted literally, not as variable
2. In your case eval() returns NULL, so there isn't anything to var_dump
3. Post more of your code to get the idea why you are using eval()...
There are 10 types of people in this world, those who understand binary and those who don't
mcog_esteban
Forum Contributor
Posts: 127 Joined: Tue Dec 30, 2003 3:28 pm
Post
by mcog_esteban » Tue Aug 28, 2007 8:43 am
There's not much code to post, i want to read file of strings like that
Code: Select all
$post[0] = array('title0','post text0','data0');$post[1] = array('title1','post text1','data1');...
and reproduce it on a "Array".
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Tue Aug 28, 2007 8:58 am
What about including the whole file by using standard include()/require() functions?
Last edited by
VladSun on Tue Aug 28, 2007 8:58 am, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
Steve Mellor
Forum Commoner
Posts: 49 Joined: Thu Aug 02, 2007 8:18 am
Post
by Steve Mellor » Tue Aug 28, 2007 8:58 am
Ca I just ask why you want to read it like that? What is the problem you are trying to solve. There may be a different way around it.
mcog_esteban
Forum Contributor
Posts: 127 Joined: Tue Dec 30, 2003 3:28 pm
Post
by mcog_esteban » Tue Aug 28, 2007 9:08 am
Steve Mellor wrote: Ca I just ask why you want to read it like that? What is the problem you are trying to solve. There may be a different way around it.
I was trying experiments with eval(), obviously keep information in a text file like that is not the best way to do.
Mordred
DevNet Resident
Posts: 1579 Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria
Post
by Mordred » Tue Aug 28, 2007 1:48 pm
A little bird tells me you'll benefit from reading about serialize() and unserialize().
CoderGoblin
DevNet Resident
Posts: 1425 Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany
Post
by CoderGoblin » Tue Aug 28, 2007 5:28 pm
Would something like this be appropriate.
Code: Select all
foreach ($_POST['title'] as $key=>$value) {
$myvar[$key]=array('title'.$key,$_POST['title'][$key],'data'.$key);
}
If not possibly use
variable variables ...
stereofrog
Forum Contributor
Posts: 386 Joined: Mon Dec 04, 2006 6:10 am
Post
by stereofrog » Wed Aug 29, 2007 3:20 am
mcog_esteban wrote: There's not much code to post, i want to read file of strings like that
Code: Select all
$post[0] = array('title0','post text0','data0');$post[1] = array('title1','post text1','data1');...
and reproduce it on a "Array".
Code: Select all
$str = "\$post[0] = array('Field1', 'Field2', 'Field3');";
eval($str);
print_r($post);
note that in your $str the dollar sign should be escaped (or the string must be in single quotes).
CoderGoblin
DevNet Resident
Posts: 1425 Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany
Post
by CoderGoblin » Wed Aug 29, 2007 4:11 am
For future reference, if you have solved it... what is the solution you found ?
mcog_esteban
Forum Contributor
Posts: 127 Joined: Tue Dec 30, 2003 3:28 pm
Post
by mcog_esteban » Wed Aug 29, 2007 4:41 am
CoderGoblin wrote: For future reference, if you have solved it... what is the solution you found ?
I used stereofrog idea.
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Wed Aug 29, 2007 5:05 am
mcog_esteban wrote: CoderGoblin wrote: For future reference, if you have solved it... what is the solution you found ?
I used stereofrog idea.
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime."
There are 10 types of people in this world, those who understand binary and those who don't