SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Is there a split function in PHP? Like the one in CGI.

    Eg. in CGI :

    $data = 'Lyon-Male';
    ($name,$value) = split(/-/, $data);

    So $name gets Lyon and $value gets Male..

    How do I achieve this in PHP?
    Thanks to all.
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2000
    Location
    Allentown PA
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know if you can split into variables in PHP, but you can do it with another step:

    http://www.php.net/manual/function.explode.php

    $data = 'Lyon-Male';
    $array = explode("-", $data");
    $name = $array[0];
    $value = $array[1];

  3. #3
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow..
    you're amazing..thanks a million!
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  4. #4
    Non-Member
    Join Date
    Apr 2000
    Location
    Waco, Texas.
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hopefully the following code will help you a little:
    Code:
    example 1:
    $v = "rob-pengelly";
    list($first,$last) = explode('-',$v);
    echo "$first $last";
    
    output: rob pengelly
    
    example 2:
    $v = "rob.pengelly";
    list($first,$last) = split('\.',$v);
    echo "$first $last";
    
    output: rob pengelly
    
    example 3:
    $v = "test0-test1.test2,test3";
    $output = split("[-.,]",$v);
    foreach($output as $value)
    {
        print $value."\n";
    }
    
    output:
    test0
    test1
    test2
    test3
    useful url's:
    http://www.php.net/split
    http://www.php.net/explode



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
  •