[thelist] PHP: is_numeric... that's the question...

Tris beertastic at gmail.com
Thu Jan 18 04:05:29 CST 2007


Max, perfect..!
In my continuing effort to better my brain, I went to learn a little
more about what was going on with your code.

I went here:
http://www.phpbuilder.com/columns/dario19990616.php3

but could find no reference to the ' ! ' you have at the start and end
of your $pattern string...
can you let me know what they do..?

I watned to use ^ to start and $ to end... but it didn't work...

confused.. ;-p

Tris.. .




On 17/01/07, Max Schwanekamp <lists at neptunewebworks.com> wrote:
> Tris wrote:
> > =========================
> > $codeLength = strlen($usercode);
> > $getLast5Characters = substr($usercode, -5, 5);
> > $getFirst2Characters = substr($usercode, 0, 2);
> > $codeInt = is_numeric($getLast5Characters);
> > $codeAlpha = is_numeric($getFirst2Characters);
> > if (($codeLength == 7) && ($codeInt) && (!$codeAlpha)) {
> >       return "code valid";
> > } else {
> >       return "Code Invalid";
> > }
> > =========================
>
> I must be blind, because I don't see the error in your code.  However, I
> it might be easier --both to read and to debug-- to use a simple regex here.
>
> function check($usercode) {
>        /* valid pattern is 2 letters followed by 5 digits */
>        $pattern = '![a-zA-Z]{2}[0-9]{5}!';
>        return preg_match($pattern, $usercode);
> }
>
> //example
> $codes = array('F454354','4G64345','AA12345');
> foreach($codes as $c) {
> echo "$c is ".(check($c) ? 'valid': 'invalid')."<br>";
> }
>
>
> --
> Max Schwanekamp
> NeptuneWebworks.com
> 541-255-2171
>
> --
>
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester
> and archives of thelist go to: http://lists.evolt.org
> Workers of the Web, evolt !
>


-- 
When a person can no longer laugh at himself, it is time for others to
laugh at him.
Thomas Szasz



More information about the thelist mailing list