TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-29-2007, 01:45 AM   #1 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default 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 :(
Haris is offline  
Reply With Quote
Old 09-29-2007, 09:12 AM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Can you put these patterns into context? It might be more a problem with how you're using them than than the patterns themselves.
Salathe is offline  
Reply With Quote
Old 09-29-2007, 12:53 PM   #3 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Can you elaborate your requirements?
Haris is offline  
Reply With Quote
Old 09-29-2007, 01:24 PM   #4 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

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";
}

?>
Karl is offline  
Reply With Quote
Old 09-29-2007, 01:37 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Don't forget the .org.uk extension which is 7 :)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-29-2007, 02:00 PM   #6 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

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. :(
Haris is offline  
Reply With Quote
Old 09-29-2007, 03:18 PM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:07 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design