View Full Version : Split Apart Data Being Received - Newbie Help

10-19-2012, 08:53 PM
I'm currently receiving some data into a single database field as shown below:

SomeVariable1 is string of text and it can have commas hence it's being separated by tilde ~ SomeVariable2, SomeVariable3, SomeVariable4

The data I'm receiving is a combination of various variables dumped into a single databse field. The first variable is separated by a ~ (tilde) because the first variable has customer comments and can include commas also. The rest of the variables are separated by commmas.

What I need to do is split apart the string so I can display them on a web page separately in different columns.

What I really want to do is along the lines:

$variable1 = All text before tilde

$variable2 = SomeVariable2

$variable3 = SomeVariable 3

$variable4 = SomeVariable 4

How can I accomplish this?

I'm a newbie in PHP and I'd appreciate any help.

10-19-2012, 10:22 PM
Use the explode() (http://php.net/manual/en/function.explode.php) function:

//Explode() separates the string into a numerically indexed array starting from 0

$separate1 = explode('~', $variable1);
$variable2 = $separate1[0]; //The variable before the ~
//$separate1[1] has the second part of the original variable after removing
//the part before the ~
//You can specify your own variables using list()
//e.g list($str1, $str) = explode('~', $variable1);
$separate2 = explode(',', $separate[1]);
$variable2 = $separate2[0]; //The variable before the first comma
$variable3 = $separate2[1]; //The 3rd variable