anolan13 Posted December 31, 2007 Share Posted December 31, 2007 Hi, Using PHP and MySQL to get user information. I have a textarea field (html) and I ask for people to list their favorite animals, seperated by commas and spaces. Example, a user may enter something like this: monkeys, dogs, tigers We all know though that this textarea is going to enter it into the database(MySQL) as one variable say...$animals. and $animals = monkeys, dogs, tigers What if I want it to automatically split those into three different variables? $animal1 = monkeys $animal2 = dogs $animal3 = tigers I imagine it would use loops, and something else. That something else is what confuses me. Can this even be done? I know this is a little confusing but any help would be greatly appreciated thank you. Quote Link to comment https://forums.phpfreaks.com/topic/83893-solved-splitting-up-textarea-forms-into-different-variables/ Share on other sites More sharing options...
Dane Posted December 31, 2007 Share Posted December 31, 2007 Hey, I think i can help. <?php $animal = "monkeys dogs tigers"; $pieces = explode(" ", $animal); echo $pieces[0]; // piece1 echo $pieces[1]; // piece2 ?> Or if they're seperated by commas <?php $animal = "monkeys,dogs,tigers"; $pieces = explode(",", $animal); echo $pieces[0]; // piece1 echo $pieces[1]; // piece2 ?> Quote Link to comment https://forums.phpfreaks.com/topic/83893-solved-splitting-up-textarea-forms-into-different-variables/#findComment-426940 Share on other sites More sharing options...
hitman6003 Posted December 31, 2007 Share Posted December 31, 2007 use the explode command... http://www.php.net/explode $animals = explode(", ", $_POST['animals']); Quote Link to comment https://forums.phpfreaks.com/topic/83893-solved-splitting-up-textarea-forms-into-different-variables/#findComment-426941 Share on other sites More sharing options...
Yesideez Posted December 31, 2007 Share Posted December 31, 2007 I'd use this if you want to remove the spaces as well: $animals=explode(',',$_POST['animals']); for ($i=0;$i<count($animals);$i++) { $animals[$i]=trim($animals[$i]); } Quote Link to comment https://forums.phpfreaks.com/topic/83893-solved-splitting-up-textarea-forms-into-different-variables/#findComment-426950 Share on other sites More sharing options...
kenrbnsn Posted December 31, 2007 Share Posted December 31, 2007 Yet another way: <?php $str = "monkeys ,dogs , tigers"; $animals = array_map("trim",explode(',',$str)); echo '<pre>' . print_r($animals,true) . '</pre>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/83893-solved-splitting-up-textarea-forms-into-different-variables/#findComment-426992 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.