SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot daemon's Avatar
    Join Date
    Apr 2001
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi everyone!
    I am looking for the php equivalent for the c/c++ function
    strtok() or a way to get similar results.
    I have looked through the php doc but couldn't find something appropriate.
    What I wanna do is:
    from a string like "ABC-DE-123" extract the "ABC" the "DE" and "123".
    Any ieas how to realise this in PHP would be helpful.

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    array explode (string separator, string string [, int limit])
    http://www.php.net/manual/en/function.explode.php

    explode() accepts a string which is the token seperator and the tokenised string (and an optional arguement which is to limit the number of tokens to extract from the string). It returns the tokens as an array. Eg:
    PHP Code:
    $tokenString 'ABC-DE-123';
    $tokenArray explode('-'$tokenString); 

  3. #3
    SitePoint Zealot daemon's Avatar
    Join Date
    Apr 2001
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot! That was exactely what i was looking for.

  4. #4
    SitePoint Zealot cokeman's Avatar
    Join Date
    Dec 2000
    Location
    So. California
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    strtok should work too..

    $string = "This-is-an-example-string";
    $tok = strtok ($string,"-");
    while ($tok) {
    echo "Word=$tok<br>";
    $tok = strtok ("-");
    }

    I would agree with Freakysid though and use split or explode -- much cleaner looking

  5. #5
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use explode() over split() in such a simple case, as split() should be used if you plan to make use of regular expressions, but is a very mild waste if you don't.


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
  •