Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

simple preg_match not working.

Posted on 2015-02-03
5
Medium Priority
?
255 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
[X]
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
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 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 84

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 84

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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 i…
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

604 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