View Single Post
Old 09-29-2007, 01:24 PM   #4 (permalink)
Karl
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