TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Regular Expressions (http://www.talkphp.com/general/1254-regular-expressions.html)

Haris 09-29-2007 01:45 AM

Regular Expressions
 
I'm having trouble with regular expressions. I think I have done something wrong within them since they're accepting the data even if they don't match the pattern.

$pattern = '/^[0-9]{3}$/'; suppose to match 000
$pattern = '/^[0-9]{3}\-[0-9]{3}\-[0-9]{3}$/'; suppose to match 000-000-00
$pattern = '/^[0-9]{5}$/'; suppose to match 00000
$pattern = '/^www\.[a-zA-Z0-9\-]+\.([a-zA-Z\.]{2,4})$/'; - suppose to match www.domain.ext.(w/e)

Help :(

Salathe 09-29-2007 09:12 AM

Can you put these patterns into context? It might be more a problem with how you're using them than than the patterns themselves.

Haris 09-29-2007 12:53 PM

Can you elaborate your requirements?

Karl 09-29-2007 01:24 PM

I think Salathe was trying to ask how you were trying to use these patterns, as some of them seem to be fine. Here's a quick script I wrote to test each pattern:

PHP Code:


<?php

// Test Expression 1

$szTest        '000';

// Works fine
$szPattern    '/^[0-9]{3}$/';

if (
preg_match($szPattern$szTest))
{
    echo 
"Match 1 ok";
}

// Test Expression 2

$szTest        '000-000-00';

// Minor error, see below
$szPattern    '/^[0-9]{3}\-[0-9]{3}\-[0-9]{3}}$/';

// Mine
// Minor change, you had the last clause of the pattern
// looking for 3 numbers, not 2
$szPattern    '/^[0-9]{3}-[0-9]{3}-[0-9]{2}$/';

if (
preg_match($szPattern$szTest))
{
    echo 
"Match 2 ok";
}

// Test Expression 3

$szTest        '00000';

// No change required, works fine
$szPattern    '/^[0-9]{5}$/';

if (
preg_match($szPattern$szTest))
{
    echo 
"Match 3 ok";
}

// Test Expression 4

$szTest        'www.talkphp.com';

// Few changes required, see below
$szPattern    '/^www\.[a-zA-Z0-9\-]+\.([a-zA-Z\.]{2,4})$/';

// The main problem here is:
// 1) the domain part of the URL is expecting a dot .
// 2) we had an extra close parentheisis character
$szPattern    '/^www\.[a-zA-Z0-9\-]+\.[a-zA-Z]{2,4}$/';

if (
preg_match($szPattern$szTest))
{
    echo 
"Match 4 ok";
}

?>


Wildhoney 09-29-2007 01:37 PM

Don't forget the .org.uk extension which is 7 :)

Haris 09-29-2007 02:00 PM

Example use:

PHP Code:

        /*
         * Summary:     Validates the US zip code format(00000)
         * Parameters:  Field name | Error message
         * Return:      Returns true if field matches the US zip code format
        *               Returns false is field doesn't match the US zip code format
         */
    
        
function isUSZipCode($field$msg)
        {
            
$value $this->getValue($field);
            
$pattern '/^[0-9]{5}$/';
            if(
preg_match($pattern$value))
            {
                return 
true;
            }
            else
            {
                
$this->aErrorList[] = array('field' => $field'value' => $value'error' => $errormsg);
                return 
false;
            }
        } 

I guess my patterns are right but I still can't think why they are accepting the data. :(

Wildhoney 09-29-2007 03:18 PM

I've had to shrink down the function a lot as it's getting values from your class. This works fine for me though which is a skeleton adaptation of your function:

PHP Code:

function isUSZipCode($iZip)
{
    if(
preg_match('/^[0-9]{5}$/'$iZip))
    {
        return 
true;
    }
    
    return 
false;
}

var_dump(isUSZipCode(50266)); 

I would hazard a guess that $this->getValue($field); is returning a value that is not a zip code - var_dump($field); it and see.


All times are GMT. The time now is 09:18 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0