Welcome Guest, Not a member yet? Register   Sign In
Regular Expression working on regexr but not in PHP
#1

(This post was last modified: 06-13-2015, 08:15 AM by FlevasGR.)

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?
Reply
#2

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.
Reply
#3

I think you should append and prepend '#' in your string instead of '/'
NexoPOS 2.6.2 available on CodeCanyon.
Reply
#4

Yeah I missed it
NexoPOS 2.6.2 available on CodeCanyon.
Reply
#5

(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.
Reply
#6

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
Reply
#7

(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 ()?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB