Jump to content

[SOLVED] Splitting up textarea forms into different variables


Recommended Posts

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.

 

 

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
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.