SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check whether string contains numbers

    Hi Guys!

    I need to check whether a string contains numbers. What would be the best way to achieve this.

    For example:

    Code:
    xxx123   would return true
    xxx        would return false
    x1x2x3   would return true
    123       would return true
    Any ideas?

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if(=== preg_match('~[0-9]~'$string)){
        
    #has numbers

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You could use a Regular Expression solution:
    PHP Code:
    function ContainsNumbers($String){
        return 
    preg_match('/\d/'$String) > 0;

    Edit:

    Darn, beaten to it!
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Or if you want something that will allow you to spend some quality time later on deciding exactly what is going on, you can use the completely unobvious choice of strpbrk.

    PHP Code:
    // TRUE if $subject contains a decimal digit
    strpbrk($subject'1234567890') !== FALSE 
    Off Topic:

    Does no-one like just casting to bool?.. *ponders*
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:


    Quote Originally Posted by Salathe View Post
    Off Topic:

    Does no-one like just casting to bool?.. *ponders*
    I don't, I prefer to be a little more explicit. Given php's loose typing, or rather the handling of it, I do worry that the casting behaviour is somewhat uncertain.

    strpbrk() huh? I wonder what it stands for... It looks like 'Spring Break'.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    > I don't, I prefer to be a little more explicit. Given php's loose typing, or rather the handling of it, I do worry that the casting behaviour is somewhat uncertain.

    Could you give any examples to clarify your uncertainty? Casting the result to bool will do, to my understanding, the same as your comparison to 1 (since TRUE will never be returned); I'm not sure where your worries over loose typing come into play?

    > strpbrk() huh? I wonder what it stands for... It looks like 'Spring Break'.

    I cannot answer this authoritatively but I believe comes from "string [return] pointer [to] break" (see GNU C lib manual page for string search functions). As logical a function name as any.
    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
  •