Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
10-19-2012, 08:53 PM #1
- Join Date
- Feb 2011
- Thanked 0 Times in 0 Posts
Split Apart Data Being Received - Newbie Help
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 #2
Use the explode() function:
//Explode() separates the string into a numerically indexed array starting from 0
$separate1 = explode('~', $variable1);
$variable2 = $separate1; //The variable before the ~
//$separate1 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);
$variable2 = $separate2; //The variable before the first comma
$variable3 = $separate2; //The 3rd variable