Got problem with preg_match()
Moderator: General Moderators
Got problem with preg_match()
I want to validate user input for fixed length string.
It's 2, 6 or 9 characters length string.
The pattern is :
right pattern :
dd : 02
dd.ddd : 02.050
dd.ddd.dd : 02.050.10
wrong pattern :
a2
20.
20..10
etc.
I've already tried some pattern
$pat1 = "/\b\d{2}(\b|\.\d{3})(\b|\.\d{2})/";
and
$pat2 = "/\b\d{2}(\.\d{3})?(\.\d{2})?/";
but the preg_match() still return true for wrong pattern case 2,... (it makes false return for case 1 though)
I know the second one is not so good, but did I make mistake for $pat1 ???
It's 2, 6 or 9 characters length string.
The pattern is :
right pattern :
dd : 02
dd.ddd : 02.050
dd.ddd.dd : 02.050.10
wrong pattern :
a2
20.
20..10
etc.
I've already tried some pattern
$pat1 = "/\b\d{2}(\b|\.\d{3})(\b|\.\d{2})/";
and
$pat2 = "/\b\d{2}(\.\d{3})?(\.\d{2})?/";
but the preg_match() still return true for wrong pattern case 2,... (it makes false return for case 1 though)
I know the second one is not so good, but did I make mistake for $pat1 ???
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
#(?<!\d|\.)\d{2}(?:\.\d{3}(?:\.\d{2})?)?(?!\d|\.)#Code: Select all
[feyd@home]>php -r "foreach($argv as $arg) var_dump($arg, (bool)preg_match('#(?<!\d|\.)\d{2}(?:\.\d{3}(?:\.\d{2})?)?(?!\d|\.)#', $arg));" asdf 1234 12 12.34 12.345 12.345. 12.345a 12.345.6 12.345.67 1.234.5 1.234.56
string(1) "-"
bool(false)
string(4) "asdf"
bool(false)
string(4) "1234"
bool(false)
string(2) "12"
bool(true)
string(5) "12.34"
bool(false)
string(6) "12.345"
bool(true)
string(7) "12.345."
bool(false)
string(7) "12.345a"
bool(true)
string(8) "12.345.6"
bool(false)
string(9) "12.345.67"
bool(true)
string(7) "1.234.5"
bool(false)
string(8) "1.234.56"
bool(false)- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Code: Select all
$input = array('20', '20.505', '20.505.01');
$regex = '/^\d{2}(?:\.\d{3}(?:\.\d{2})?)?$/';
foreach ($input as $i => $v) {
$match = array();
preg_match($regex, trim($input[$i]), $match);
var_dump($match);
echo '<br />';
}- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Non-capturing subpattern.
When you group things with () you create subpatterns with separate results in the $match variable, putting ?: prevents this from happening and lumps them all together still.
When you group things with () you create subpatterns with separate results in the $match variable, putting ?: prevents this from happening and lumps them all together still.
Code: Select all
$nonCapturing = '/^\d{2}(?:\.\d{3}(?:\.\d{2})?)?$/';
$capturing = '/^\d{2}(\.\d{3}(\.\d{2})?)?$/';
preg_match($nonCapturing, '20.505.01', $match);
var_dump($match);
preg_match($capturing, '20.505.01', $match);
var_dump($match);Code: Select all
array(1) {
[0]=>
string(9) "20.505.01"
}
array(3) {
[0]=>
string(9) "20.505.01"
[1]=>
string(7) ".505.01"
[2]=>
string(3) ".01"
}I tried it yesterday and it worked....Non-capturing subpattern.
When you group things with () you create subpatterns with separate results in the $match variable, putting ?: prevents this from happening and lumps them all together still
Thanks, guys your the best...
but I still don't get what happened, can you explain it to me?
and where can I find pattern validation for:
date (for all format, e.g. dd-mm-yy; mm, dd yyy; etc.)
email address
SQL injection prevention
and others
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
- The patterns above can be altered to your date formats
- email addresses are fairly complicated, but luckily we had a user that has an RFC compliant regex.
http://svn.gna.org/viewcvs/blacknova/tr ... iew=markup - SQL injection filters will depend greatly on what data you are expecting and what data the database is expecting. Generally, mysql_real_escape_string() or your database's equivalent are sufficient to prepare the data, but not to make sure it conforms to the expected value.
- and other what?