Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2011
    Posts
    32
    Thanks
    3
    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.

  • #2
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    335
    Thanks
    2
    Thanked 49 Times in 48 Posts
    Use the explode() function:

    PHP Code:

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

    $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

    ?>


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •