SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Split a string with a period

    How would I split this string:
    PHP Code:
    string'item.35' 
    So it comes out like this:
    PHP Code:
    $type 'item';
    $id '35'
    I know there are a lot of string fucntions but I have no idea what would be easiest.
    Maybe implode(), an array would be fine

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    explode();
    or
    split();
    or one of the other string functions.

    Implode does the opposite
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay cool, thanks.
    I was looking up strsplit() and forgot implode/explode, yeah its explode lol.

    I did this:
    PHP Code:
    $a explode('.'$id);
    echo 
    $a[0];
    echo 
    $a[1]; 
    That will work!

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    There are a multitude of different ways to achieve such a simple task. Here's a few (you already know about the first one):

    PHP Code:
    // 1. EXPLODE! *pop*
    list($type$id) = explode('.''item.35'2);

    // 2. Scan a formatted string
    list($type$id) = sscanf('item.35''%[a-z].%d'); 
    Salathe
    Software Developer and PHP Manual Author.


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
  •