Problems with preg_match

Hi Forum

I hope you can help Me.

I just upgraded from php 5.6 to 7. Now I have problems with a script in one of my forms on my Joomla site.

For a long time I used this script:

[php]function cpr($cpr)
{ if ($cpr == ‘’) return true;
if (eregi(’(((([0-2][1-9]|[12]0)(0[1-9]|1[0-2]))|(30(0[13-9]|1[0-2]))|(31(0[13578]|1[02])))[0-9]{6})|0000000000’,$cpr)) return true;
else return false; }
[/php]
But it ain’t working anymore.

I have tried to change erigi with preg_match. But no luck…

Can anyone “upgrade” my script to preg_match?

Best Regards

Pelle

Please. Any ideas?

Best Regards

Pelle

So, you are saying that this,

[php]function cpr($cpr)
{
if ($cpr == ‘’)
return true;
if (preg_match(’(((([0-2][1-9]|[12]0)(0[1-9]|1[0-2]))|(30(0[13-9]|1[0-2]))|(31(0[13578]|1[02])))[0-9]{6})|0000000000’, $cpr))
return true;
else
return false;
}[/php]
Doesn’t work?

I am afraid so.

But I didn’t use the same line breaks as you.

You use 9 lines and I use 4 lines. Is this of importance?

Best Regards

Pelle

Your formatting looks like the it uses indian indention, where mine is readable.

[php] function cpr($cpr)
{
if ($cpr == ‘’)
return true;
if (preg_match(’/(((([0-2][1-9]|[12]0)(0[1-9]|1[0-2]))|(30(0[13-9]|1[0-2]))|(31(0[13578]|1[02])))[0-9]{6})|0000000000/i’, $cpr))
return true;
else
return false;
}[/php]

The above should work. HOWEVER, not really sure why you are grouping the numbers when you don’t use them.

Great. I will test your version tomorrow - it is late in Denmark.

Regarding the numbers I simply try to test if a number could be a social security number:

ddmmyyxxxx

If there is a smarter way to do this I am very interested :slight_smile:

I manly work with design and text. I am pretty helpless when it comes to coding.

Best Regards

Pelle

So, 10 digits, number only? or is there a specific format?

10 digits

Social Security No.
First 6 digits day month year - (and it has to be real dates. Max 31 days and 12 months)
The last 4 digits whatever - from 0000 to 9999

I am not trying to test if the number actually is a social security number, I only want to test if the number fits the description above.

Best regards

Pelle

If you are passing the values in as strings, the above preg_match will work. If not, leading zero’s are trimmed.

Thanks! Everything is working again.

It was a question of structure.

Somehow php7 is more sensitive than php5?

Best Regards

Pelle

I think this is a good example where a longer solution might be worth considering. I believe it’s much more important to get code readable and understandable, simply reading the code should tell you what it does. And you have to either spend some time with those regexes - or be a bit too comfortable with regexes in order to see what’s actually going on.

[php]/**

  • @param string $string

  • @return bool
    */
    function validateSocialSecurityNumberFormat($string)
    {
    if(strlen($string) !== 10) return false;

    list($date, $number) = str_split($string, 6);
    $d = DateTime::createFromFormat(‘dmy’, $date);

    return $d && $d->format(‘dmy’) === $date && is_numeric($number);
    }

var_dump(validateSocialSecurityNumberFormat(‘0305161234’));[/php]

Changed the function name to what I think this actually does. Since it doesn’t actually check if it is a valid social security number, I landed on validating the format.

Removed the line that made it valid if no string was submitted. If the application elsewhere doesn’t care if it’s valid if it’s not submitted, then do that check outside this function.

Changed the code to split the string and do more readable (??) checks on the format.

Hi Jim

You are absolutely right.

My main interest is design and text, therefore I sometimes search for easy/fast solutions regarding coding. And they are not the best solutions in the long run.

Quite often I don’t understand the scripts I am using. I simply google, combine and test until things work…

In the future i am planning to work closer together with people who actually can code - I am sure it will give better and more bulletproof results. :wink:

Thanks for your input. I will try to implement the solution.

Best regards

Pelle

epreg was deprecated out of 5.6. In 7 they actually removed what was deprecated.

Sponsor our Newsletter | Privacy Policy | Terms of Service