Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Php Question

Options
  • 06-01-2005 12:57am
    #1
    Registered Users Posts: 648 ✭✭✭


    i know this is probably not the best place.but here goes.

    i have a variable

    $variable = "one;two;three;four;five";

    how do i go about getting the numbers out seperately...


    tnx


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Use an array in its place? But in the case that this isn't possible, take a look into the string Tokenizer.


  • Registered Users Posts: 648 ✭✭✭ChicoMendez


    Use an array in its place? But in the case that this isn't possible, take a look into the string Tokenizer.


    Cheers, cant use an array as "one;two;three;four;five" will be read from a database field.


  • Closed Accounts Posts: 4,655 ✭✭✭Ph3n0m


    Try this

    [PHP]
    <?php
    // Example 1
    $variable = "one;two;three;four;five";
    $pieces = explode(";", $variable);
    echo $pieces[0]; // one
    echo $pieces[1]; // two
    echo $pieces[2]; // three
    echo $pieces[3]; // four
    echo $pieces[4]; // five

    ?>
    [/PHP]

    or the following example allows you to name each of the results you will explode (handy if you know exactly how many results will be placed into the variable

    [PHP]
    <?php
    $data = "one;two;three;four;five";
    list($first, $second, $third, $fourth, $fifth) = explode(";", $data);
    echo $first; // one
    echo $second; // two
    echo $third; // three
    echo $fourth; // four
    echo $fifth; // five

    ?>
    [/PHP]


  • Registered Users Posts: 7,277 ✭✭✭kenmc


    yeah tokenizer is the way to go all right....

    $string = "one;two;three;four;five";
    $tok = strtok($string,";");
    while ($tok) {
    echo "Word=$tok<br>";
    $tok = strtok(";");
    }
    can put that into an array then instead of printing it out... or whatever you wanna do


Advertisement