php check for digits

Posted on 2012-04-07
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?


Question by:MK15
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 83

Assisted Solution

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

$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

LVL 83

Expert Comment

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

<title>PHP Digits</title>
<h1>PHP Digits</h1>
$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";


Open in new window

LVL 18

Accepted Solution

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


Author Comment

ID: 37821310
cheers guys they both did the trick

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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 ( and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

751 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