Hi,
What's a solid regular expression for allowing just integers and dots in a variable?
Could do with an online reference for RegExp actually. Any kind folk know of such?
Many thanks
| SitePoint Sponsor |





Hi,
What's a solid regular expression for allowing just integers and dots in a variable?
Could do with an online reference for RegExp actually. Any kind folk know of such?
Many thanks
I quite like RegExr at the moment, a nice live flash based tester by RegExp guru Grant Skinner.PHP Code:<?php
if(1 !== preg_match('~[^0-9.]~', '1234567890.'))
{
#valid
}
?>
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.





Brilliant stuff Anthony, thanks again.
Pops into wardrobe, picks out stupid hat, then says...In this case would '1' be my variable name?
Nope, `1234567890.` would be.
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.





Aaaaah of course. And '1' just represents TRUE ?
Too late in the afternoon. Thanks once more Anthony.
No worries mate.
preg_match can return 0, 1 or false. If it finds a match it will return 1, our pattern searches for anything that is NOT numeric or a period. If it finds a match (therefore returning 1) we know the variable contains an invalid char.
Make sense?
From the manual:
preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject . preg_match() returns FALSE if an error occurred.
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
Bookmarks