Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post

    finding numbers in a string

    hi all,
    is there a simple way for extracting numbers from a string. i have a session array in which the data is like this "AB000121 --> nameof the client"
    wherein i just want to extract "000121" where 0000121 could be any number from 0 to sky is the limit [just the numbers]... how can i do it??

    Thanks in advance

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Just use preg_replace()
    PHP Code:
    <?php
    $string 
    'AB000121';
    $newstring preg_replace('/[a-zA-Z]/xms','',$string);
    echo 
    $newstring;
    ?>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    N. Ireland
    Posts
    351
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Aerospace: Can you explain the use of xms in your preg_replace?

    D.
    Daemonkin.
    If this was helpful, please add to my reputation
    Thousand Sons - Freelance Web Developer - ninetyonedegrees.com

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You can look up these things yourself you know?
    Trailing xms are three regex options
    – x : Extended formatting (whitespace in regex is ignored)
    – m : For line boundaries (and eliminates a cause of some subtle
    bugs)
    – s : ensures everything is matched by the "." symbol
    http://www.ilovejackdaniels.com/chea...s-cheat-sheet/
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post

    Million Thanks

    Hi Aerospace,
    the code works the way i want.
    Quote Originally Posted by _Aerospace_Eng_ View Post
    Just use preg_replace()
    PHP Code:
    <?php
    $string 
    'AB000121';
    $newstring preg_replace('/[a-zA-Z]/xms','',$string);
    echo 
    $newstring;
    ?>
    Thanks a lot


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •