Use ereg in scriptcase

Hi,

This my php code using ereg to check date format :
if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
echo “$regs[3].$regs[2].$regs[1]”;
} else {
echo “Invalid date format: $date”;
}

That code can runs well when I try in notepad, but when I put it in scriptcase it always return false. What am I doing wrong?

Thanks in advance…

regards,

Mellz

Re: Use ereg in scriptcase

I ran your code at:
http://writecodeonline.com/php/

Your code: adding date value


$date = '2011-10-01';
if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
echo "$regs[3].$regs[2].$regs[1]";
} else {
echo "Invalid date format: $date";
}

It returns a valid date as expected:

01.10.2011

I would suspect that SC is not handing the code properly due to the curly brackets (SC see {} as macro delimiters)
SC also uses [] as global delimiters. IMO, this us not a good selection of delimiters.

I tried adding spaces so SC did not assume it was a macro, but then it does not evaluate properly in PHP


if (ereg ("([0-9]{ 4 })-([0-9]{ 1,2 })-([0-9]{ 1,2 })", $date, $regs)) {

You may have to find another approach for this.

Regards,
Scott.

Re: Use ereg in scriptcase

You can also find the generated code to your function using grep and see if SC is creating bad code for your function.

Regards,
Scott.

Re: Use ereg in scriptcase

what do you mean by “You can also find the generated code to your function using grep”?

It can’t works when I tried adding spaces between {} and [].
I also tried to put “([ 0-9 ]{ 4 })-([ 0-9 ]{ 1,2 })-([ 0-9 ]{ 1,2 })” into a variable, this way also can’t works…

Do you have any other idea? need help please…

Regards,
Mellz

Re: Use ereg in scriptcase

what do you mean by “You can also find the generated code to your function using grep”?

When you create your code in SC, the code is then included in the PHP generated by SC. You can look at the generated PHP file that has your code and see if SC somehow interpreted things
incorrectly and always returns false.

SC might have changed your code, but maybe not. My thought is that perhaps it did due to the numerous [] and {} in the code.

As mentioned, the spaces will not work.

Regards,
Scott.

Re: Use ereg in scriptcase

To emphasise other replies here, I have on several occasions fallen foul of SC’s code generation corrupting perfectly valid PHP source.

The work around seems to be to break the code up into smaller segments using a PHP variables as much as possible.