Forum Moderators: coopster

Message Too Old, No Replies

ereg() is deprecated in 5.3

What do I have to do to update this block of code?

         

chutes

6:18 am on Jul 2, 2010 (gmt 0)

10+ Year Member



  if (ereg('^4[0-9]{12}([0-9]{3})?$', $this->cc_number)) {
$this->cc_type = 'Visa';
} elseif (ereg('^5[1-5][0-9]{14}$', $this->cc_number)) {
$this->cc_type = 'Master Card';
} elseif (ereg('^3[47][0-9]{13}$', $this->cc_number)) {
$this->cc_type = 'American Express';
} elseif (ereg('^3(0[0-5]|[68][0-9])[0-9]{11}$', $this->cc_number)) {
$this->cc_type = 'Diners Club';
} elseif (ereg('^6011[0-9]{12}$', $this->cc_number)) {
$this->cc_type = 'Discover';
} elseif (ereg('^(3[0-9]{4}|2131|1800)[0-9]{11}$', $this->cc_number)) {
$this->cc_type = 'JCB';
} elseif (ereg('^5610[0-9]{12}$', $this->cc_number)) {
$this->cc_type = 'Australian BankCard';
} elseif (ereg('^(49|56|63|67)[0-9]{14}([0-9]{2,3})?$', $this->cc_number)) {
$this->cc_type = 'Maestro/Solo';
} else {
return -1;
}


I've tried replacing all isntances of ereg() with preg_replace() and slashing out the ' (like so):
if (preg_replace('/^4[0-9]{12}([0-9]{3})?$/', $this->cc_number)) {

but it causes me to get a failure every time.

chutes

7:16 am on Jul 2, 2010 (gmt 0)

10+ Year Member



Solution found!

if (preg_match('/^4[0-9]{12}([0-9]{3})?$/', $this->cc_number)) {


I was adding a / at the very end, beside the $ where it wasn't needed.
Although I don't understand the difference; it works!

Matthew1980

7:23 am on Jul 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there chutes,

In the code you have, your using ereg, which returns either true or false depending on the conditions & whether it meets them, so, using preg_replace wouldn't function as you need to match then replace - surely you would need preg_match, which is functionally the same as ereg.

So, you would need something like:-

if (preg_match('/^4[0-9]{12}([0-9]{3})?$/', $this->cc_number)) {
//match
}
else{
//no match
}

I'm no expert on regexp patterns, but you have the format about right from what little I know of it ;)

Hope this helps a little,

[EDIT:] Took me a while to type, but I'm glad you saw the error :)

Cheers,
MRb

chutes

9:08 am on Jul 2, 2010 (gmt 0)

10+ Year Member



Thanks for pointing my error out; I had no idea that I was using preg_replace instead of preg_match until you'd pointed it out!

I found the resolution posted in github so I simply copied it over and it worked.. I thought I was using the correct formula the entire time. Glad I saw your post, else the next time I encounter this error I'd be scratching my head for hours. ;)

Matthew1980

9:11 am on Jul 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there chutes,

Have a read through this: [uk.php.net ] I reference this quite a lot myself as regexp isn't a strong suite of mine, though its on the to-do list, along with lots of other things ;)

Anyway, glad your sorted now.

Cheers,
MRb

chutes

9:53 am on Jul 2, 2010 (gmt 0)

10+ Year Member



Thanks for the reference. I'm going to read up on this as I prepare my site for the 5.3 transition.

rocknbil

6:08 pm on Jul 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's lots of shortcuts here. :-)

From the PHP manual [php.net]

g1smd's recent post [webmasterworld.com] (June 6 2010) with easy recommendations for weaning from ereg

Coopster's post, 2009 [webmasterworld.com] list of deprecated e-like functions