Reply to Re: Easy ereg() question

Your name:

Reply:


Posted by Mike Willbanks on 09/18/05 01:26

Liam,

> I'm trying to make sure a form has only one or two digits in either of
> two fields.
> I looked at php.net and
> http://www.regular-expressions.info/reference.html and this is what I
> put together, but while it successfully refuses any non-digit, it still
> lets any number of digits through.
>
> Any suggestions?
>
> if ((ereg("([0-9]{1,2})", $people))&&(ereg("([0-9]{1,2})", $rooms))) {

Instead of ereg you should always use preg_match because it is faster
than ereg. Also part of your reason that it is matching any number of
digit is because what you are actually asking for is to test that there
is one or two digits in the rule...

Try the following:

if (preg_match('^[0-9]{1,2}$', $people) && preg_match('^[0-9]{1,2}$',
$rooms))

The ^ defines the start of a string and the $ defines the end of a string.

--
Mike Willbanks
Zend Certified Engineer
http://www.digitalstruct.com

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация