Results 1 to 2 of 2

Thread: Splitting strings

  1. #1
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default Splitting strings

    Hello everyone,
    I'm trying to split one string into several, but not sure how...

    Lets say I have a string variable

    Code:
    $var = 'test@gmail.com~|~test@hotmail.com~|~test@grapevine.com';
    How would I split that into three different strings... eg.

    Code:
    $var1 = 'test@gmail.com';
    $var2 = 'test@hotmail.com';
    $var3 = 'test@grapevine.com';
    ~|~ is the divider I want to use....
    Any help?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    It's easier to just explode it and use the resulting array:

    PHP Code:
    <?php 
    $var 
    'test@gmail.com~|~test@hotmail.com~|~test@grapevine.com'
    $vars explode('~|~'$var); 
    echo 
    implode('<br>'$vars); 
    ?>
    But if you need individual variables, you could do a foreach on the array to make them:

    PHP Code:
    <?php
    $var 
    'test@gmail.com~|~test@hotmail.com~|~test@grapevine.com';
    $vars explode('~|~'$var);
    foreach(
    $vars as $key => $val){
        
    $k $key 1;
        
    $GLOBALS["var$k"] = $val;
    }
    echo 
    $var1 '<br>';
    echo 
    $var2 '<br>';
    echo 
    $var3 '<br>';
    ?>
    Last edited by jscheuer1; 07-26-2012 at 04:27 AM. Reason: found way to assign to vars
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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