1

I have this regular expression and I want to replace the "lb" part with the parameter that's being passed in with what ever weight they want to use so what I have is this

protected function validateCheckWeight($attribute, $value, $parameters) 
{        
    // this always outputs correctly either true or false
    return preg_match('/^(?!0\d|0lb$)\d+(?:\.\d+)?lb$/', $value);
}

and what I want is this but it doesn't work when doing validation

protected function validateCheckWeight($attribute, $value, $parameters) 
{        

    return return preg_match('@/^(?!0\d|0' . $parameters[0] .'$)\d+(?:\.\d+)?' . $parameters[0] . '$/@', $value);    
}



protected function validateCheckWeight($attribute, $value, $parameters) 
{   
    dd('@/^(?!0\d|0' . $parameters[0] .'$)\d+(?:\.\d+)?' . $parameters[0] . '$/@');
    // output "@/^(?!0\d|0kg$)\d+(?:\.\d+)?kg$/@"
    return preg_match('@/^(?!0\d|0' . $parameters[0] .'$)\d+(?:\.\d+)?' . $parameters[0] . '$/@', $value);
    // this always outputs false

}
5
  • 1
    What exactly does "doesn't work" mean? You need to supply a test case. What values are you passing in for $attribute, $value and $parameters, and what return value are you expecting? Commented Mar 12, 2016 at 22:48
  • BTW: It looks like the $attribute argument is never used. Commented Mar 12, 2016 at 22:50
  • it returns true of false and $parameters[0] is the weight type e.g. kg or lbs Commented Mar 12, 2016 at 22:50
  • And what are you passing in for $value? Commented Mar 12, 2016 at 22:50
  • value being passed in is 12kg Commented Mar 12, 2016 at 22:53

2 Answers 2

1

You have 2 different delimiters in your regex, @ and /. I've just confirmed that if you remove @ from the beginning and end of your regular expression, your function returns true for the inputs mentioned in your comments ($value = "kg", $parameters = array('kg')).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you that was the problem I learned something new today cheers
0

If I correct understand this question you can

return strlen(parameters[0]) == strlen(rtrim(parameters[0],'lb'));

but I think again don't correct understand this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.