Solved

php check for digits

Posted on 2012-04-07
4
261 Views
Last Modified: 2012-04-08
hi ,

I need to be able to check the first 9 digits of a string are numbers, how would i do this?

pass
123456783x
359372910flasd
fail
3k33j52l4
asd3asd323
asd2345343213

thanks
0
Comment
Question by:MK15
  • 2
4 Comments
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 37819818
Use 'ctype_digit' and 'substr'.

<?php
$strings = array('pass', '123456783x', '359372910flasd','fail','asd2345343213');
foreach ($strings as $testcase) {
    if (ctype_digit(substr($testcase, 0, 9))) {
        echo "The string $testcase consists of all digits.\n";
    } else {
        echo "The string $testcase does not consist of all digits.\n";
    }
}
?>

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37819836
This is a better version:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHP Digits</title>
</head>
<body>
<h1>PHP Digits</h1>
<?php
$strings = array('pass', '123456783x', '359372910flasd','fail','asd2345343213');
foreach ($strings as $testcase) {
    if (ctype_digit(substr($testcase, 0, 9))) {
        echo "Pass - The string '$testcase' does start with 9 digits.<br>\r\n";
    } else {
        echo "Fail - The string '$testcase' does Not start with 9  digits.<br>\r\n";
    }
}
?>

</body>
</html>

Open in new window

0
 
LVL 18

Accepted Solution

by:
Andrej Pirman earned 250 total points
ID: 37819842
$string = "4534563436nmrer";
$first_part = substr($string, 0, 9);
if (intval($first_part)==$first_part) { 
   echo "First 9 chars are numerical"; 
} else {
   echo "Not numerical";
}

Open in new window

0
 

Author Comment

by:MK15
ID: 37821310
cheers guys they both did the trick
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now