SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Dec 2004
    Location
    Charlotte
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Splitting a string.

    Hello,

    I want to seperate the following string : m23423.

    I would like to seperate it so,

    $type = m;
    $url = 23523;

    How would I go about doing this? Thanks.

  2. #2
    SitePoint Evangelist dmsuperman's Avatar
    Join Date
    Feb 2005
    Location
    A box
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You want to seperate only that string, or strings like that?
    Also, do you want to do it so it's the first character is one string, and one is the url? Or do you want them to be split up by letters and numbers?

    Here are a couple solutions (with $string equal to "m23423"):
    $type = preg_replace("/[^a-zA-Z]/i", "", $string);//This will set type equal to all of the letter characters of $string, uppercase and lowercase
    $url = preg_replace("/[^0-9]/i", "", $string);//This will set url equal to all of the number characters of $string
    $type = $string{0};//This will return the first character of string
    $url = substr($string, 1, strlen($string)-1);//This will set $url equal to all of the characters past the first one

    The problem with the first one is m2424e will return me, and same with the first url bit, m34e6 will return 346. Anything like !#@$@$#^ will just be left alone.
    <(^.^<) \(^.^\) (^.^) (/^.^)/ (>^.^)>
    Core 2 Duo E8400 clocked @ 3.375GHz, 2x2GB 800MHz DDR2 RAM
    5x SATA drives totalling 2.5TB, 7900GS KO, 6600GT

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2005
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to split it only to the first character use the substring function:

    $type=subst("m23423",0,1);
    $url = subst("m23423",1);

    Hope that helps

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dmsuperman
    $url = substr($string, 1, strlen($string)-1);//This will set $url equal to all of the characters past the first one
    By default, if you don't include a length parameter, substr will grab everything after the offset, so this would do the trick as well: $url = substr($string, 1).
    Last edited by k0ng; May 7, 2005 at 14:25. Reason: As superuser pointed out...

  5. #5
    SitePoint Evangelist dmsuperman's Avatar
    Join Date
    Feb 2005
    Location
    A box
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by k0ng
    By default, if you don't include a length parameter, substr will grab everything after the offset, so this would do the trick as well: $url = substr($string, 1).
    Meh, forgot that.
    <(^.^<) \(^.^\) (^.^) (/^.^)/ (>^.^)>
    Core 2 Duo E8400 clocked @ 3.375GHz, 2x2GB 800MHz DDR2 RAM
    5x SATA drives totalling 2.5TB, 7900GS KO, 6600GT

  6. #6
    SitePoint Addict
    Join Date
    Dec 2004
    Location
    Charlotte
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I didn't know it was that easy.


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
  •