CodeIgniter Forums
Regular Expression working on regexr but not in PHP - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Regular Expression working on regexr but not in PHP (/showthread.php?tid=62147)



Regular Expression working on regexr but not in PHP - FlevasGR - 06-13-2015

Hello, i want to use regex to validate the coupon format. I wrote the rules on regexr.com and it worked fine but not in php.

The expression i have: ([0-9]{3})+(-)+([0-9A-Z]{3})+(-)+([A-Z]{3})

Coupon format:

230-4WE-GAG

424-423-NJB

Basically the first 3 digits are numbers followed by a dash then 3 alphanumeric digits followed by a dash and at the end 3 characters

And the code i have:

PHP Code:
if (preg_match('/([0-9]{3})+(-)+([0-9A-Z]{3})+(-)+([A-Z]{3})/'$coupon)) {            
     
$coupon_data $this->coupons->check_coupon($coupon);
     
$this->response($coupon_data200);

(i'm using the rest library)

I know that PHP treats regex a little bit different but in which way?


RE: Regular Expression working on regexr but not in PHP - CroNiX - 06-13-2015

The regex in your php code is not the same regex you posted in "The expression i have". Check the parenthesis. You're missing some.


RE: Regular Expression working on regexr but not in PHP - Blair2004 - 06-13-2015

I think you should append and prepend '#' in your string instead of '/'


RE: Regular Expression working on regexr but not in PHP - Blair2004 - 06-13-2015

Yeah I missed it


RE: Regular Expression working on regexr but not in PHP - FlevasGR - 06-13-2015

(06-13-2015, 08:14 AM)CroNiX Wrote: The regex in your php code is not the same regex you posted in "The expression i have". Check the parenthesis. You're missing some.

Sorry, i made a little mistake as i was pasting this on the forum. The expression is exactly the same.


RE: Regular Expression working on regexr but not in PHP - CroNiX - 06-13-2015

Try
PHP Code:
preg_match('/[0-9]{3}-[A-Z0-9]{3}-[A-Z]{3}/'$coupon

3 digits
followed by dash
followed by mixture of 3 digits and/or A-Z chars
followed by dash
followed by 3 chars A-Z


RE: Regular Expression working on regexr but not in PHP - FlevasGR - 06-13-2015

(06-13-2015, 08:31 AM)CroNiX Wrote: Try
PHP Code:
preg_match('/[0-9]{3}-[A-Z0-9]{3}-[A-Z]{3}/'$coupon

3 digits
followed by dash
followed by mixture of 3 digits and/or A-Z chars
followed by dash
followed by 3 chars A-Z

It worked perfectly. So. php doesn't like the ()?