Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Php Question

  • 06-01-2005 12:57AM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 7,290 ✭✭✭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