#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171

    Easiet way to check if a string contains numbers or not.


    Hi;

    What is the easiet way to check if a string contains numbers or not?

    Thank you
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Only digits or at least one digit?

    PHP Code:
    <?php

    $test 
    '123';

    // at least one digit
    var_dumppreg_match('/\\d/'$test) );

    // only digits
    var_dumpctype_digit($test) );
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171
    Originally Posted by Jacques1
    Only digits or at least one digit?

    PHP Code:
    <?php

    $test 
    '123';

    // at least one digit
    var_dumppreg_match('/\\d/'$test) );

    // only digits
    var_dumpctype_digit($test) );
    Thanks. Is there anything in PHP you don't know?
  6. #4
  7. Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Location
    New Springfield, OH
    Posts
    1,214
    Rep Power
    1469
    Originally Posted by English Breakfast Tea
    Thanks. Is there anything in PHP you don't know?
    Heh. He'll never tell.

    Strictly for comparison, you could also check for all digits using a regex.
    PHP Code:
    <?php 

    $test 
    '123'

    // only digits
    var_dumppreg_match('/^\d+$/'$test) );
    I do like the other method better though as long as you are using PHP 5.1.0 or later. In earlier versions it will return true in the case of an empty string.

    And Jacques, is there a reason you're double escaping in a single quoted string?
    Don't like me? Click it.

    Scripting problems? Windows questions? Ask the Windows Guru!

    Stay up to date with all of my latest content. Follow me on Twitter!

    Help us help you! Post your exact error message with these easy tips!
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by Nilpo
    And Jacques, is there a reason you're double escaping in a single quoted string?
    It's not double escaped. It's a single escaped backslash.

    Of course you could leave out the explicit escaping in this particular case, because there's no ambiguity. But I wouldn't rely on this. In general, all backslashes should be escaped (regardless of whether it's a single quoted or a double quoted string).

    Note that your regex will accept a trailing newline character, which is probably not what you want:

    PHP Code:
    <?php

    $test 
    "123\n";

    var_dumppreg_match('/^\d+$/'$test) );
    I'd generally avoid the ^ and $ anchors to define the limits of the string, because they have different meanings depending on the regex modifiers. It's better to use \A and \z.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Location
    New Springfield, OH
    Posts
    1,214
    Rep Power
    1469
    Fair enough. Thanks.
    Don't like me? Click it.

    Scripting problems? Windows questions? Ask the Windows Guru!

    Stay up to date with all of my latest content. Follow me on Twitter!

    Help us help you! Post your exact error message with these easy tips!

IMN logo majestic logo threadwatch logo seochat tools logo