Solved

simple preg_match not working.

Posted on 2015-02-03
5
209 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 82

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 82

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
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.

757 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

21 Experts available now in Live!

Get 1:1 Help Now