Solved

simple preg_match not working.

Posted on 2015-02-03
5
214 Views
Last Modified: 2015-02-03
This code use to work. I copied it to a new program and now I keep getting - Not a number. So I removed it and put it on a page by itself and get the same - Not a number. It seems to defy logic. Can someone spot something that I missed? Thanks.

<?php

$error = '';
$id = '5';

$pattern = '/^[0-9]{10}$/';
	if (!preg_match($pattern, $id)) {$error = 'Not a number.';}

echo $error;
?>

Open in new window

0
Comment
Question by:kadin
  • 2
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 40587777
You'll want to update the pattern to this I think:
$pattern = '/^[0-9]{1,10}$/';
?>

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 40587778
The pattern you had required exactly 10 digits to match; that's what the {10} does.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40587780
I don't know what's wrong with your code but this seems to work.  I included two versions, one with a number and one with a letter.
<?php

$error = '';
$id = '5';

//$pattern = '/^[0-9]{10}$/';
$pattern = '/[0-9]/';
	if (!preg_match($pattern, $id)) {$error = 'Not a number.';}
	else {$error = 'a number.';}

echo $id." is ".$error."<br>";

$id = 'A';

//$pattern = '/^[0-9]{10}$/';
$pattern = '/[0-9]/';
	if (!preg_match($pattern, $id)) {$error = 'Not a number.';}
	else {$error = 'a number.';}

echo $id." is ".$error."<br>";
?>

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40587783
@Terry_Wood's suggestion seems to work.
0
 

Author Closing Comment

by:kadin
ID: 40587786
Thanks Terry and Dave.

I totally missed that.
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

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

947 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

22 Experts available now in Live!

Get 1:1 Help Now