SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_split how to match only number

    Hi.
    Is there a way to catch only the number
    PHP Code:
    $r preg_split('%([_])%''123_12_jjjj', -1PREG_SPLIT_NO_EMPTY);
    var_dump($r); 
    Bye

  2. #2
    SitePoint Member IdleServ's Avatar
    Join Date
    Nov 2003
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you only want the numbers wouldn't it better to use preg_match_all?

    Something like...
    PHP Code:
    preg_match_all('/[0-9]+/''123_12_jjjj'$resultPREG_PATTERN_ORDER); 
    Chris - Managing Director
    IdleServ Ltd
    IdleServ Web Hosting, Reseller Hosting & Domain Names

  3. #3
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    If you did want to use preg_split, then just split the string on non-digit values:

    PHP Code:
    print_r(
        
    preg_split('/\D/''123_12_jjjj'NULLPREG_SPLIT_NO_EMPTY)
    );

    /*
    Array
    (
        [0] => 123
        [1] => 12
    )
    */ 
    Salathe
    Software Developer and PHP Manual Author.

  4. #4
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks


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
  •