Solved

simple preg_match not working.

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

825 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