Forum Moderators: coopster

Message Too Old, No Replies

why does this reg ex cause a parse error, expecting `'}''

seems to be problem with lower bound

         

zollerwagner

7:42 pm on Feb 10, 2004 (gmt 0)

10+ Year Member



This code throws a parse error in PHP when I load the page.

function validateFixedInput($fixedInput,$inputType,$minChar,$maxChar)
{
// allow alphanumeric
// size between minChar and maxChar inclusive

if (!ereg("^[[:alnum:]]{$minChar,$maxChar}$",$fixedInput))
{
global $errMessage;
$errMessage[] = "$inputType not selected.";
}
}

If I take out the $minChar variable and substitute an integer, it works.

$minChar is defined in the calling file and passed into the function.

Also, if I replace the ereg with this, it appears to work:

if (!ereg("^[[:alnum:]]{" . $minChar . ",$maxChar}$",$fixedInput))

...although I haven't tested it to see if it really gives the results I want.
Does anyone see what the problem is?

ergophobe

7:50 pm on Feb 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




$var = hello

"string with {$var}"

treats the curly brace a flag to make a variable substitution, resulting in "string with hello"

"string with {1, $var}"

Treats the { as a literal and results in "string with {1, hello}"

Does that sufficiently explain what's happening?

zollerwagner

8:10 pm on Feb 10, 2004 (gmt 0)

10+ Year Member



Wow. That one surprised me! I don't usually use the {} string replacement scheme, so that escaped me.

Thanks for the quick answer.

In this case, then, is there another option, or do I just use the concatenation method?

ergophobe

8:21 pm on Feb 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



concatentate:
- slightly slower to code,
- slightly faster for the processor,
- no confusion.

Tom

zollerwagner

8:26 pm on Feb 10, 2004 (gmt 0)

10+ Year Member



Thanks, Tom!