Drongo_III Posted August 19, 2012 Share Posted August 19, 2012 Hi Guys Quick question. I am trying to write a sort of multi purpose validation function (just for simple forms). I want to pass a dynamic value to the length part of the regular expression patterns below. I can get this to work for the 'tstring' case in the switch statement (i.e. where i pre-concatenate the values). But if i use complex syntax and then try to pass the values separately ( as in 'tint' case below) i can't seem to get it to work. Is it possible to do? Or do i need to pre-concatenate first? Thanks chaps! function call looks like: $validate->multiVal($_POST['name'], 1, 6, 'tstring'); Actual function: public function multiVal($subject, $lenMin, $lenMax, $type){ $length = $lenMin . "," . $lenMax; switch($type){ case 'tstring': $pattern = "/^[a-zA-Z\s\b]{{$length}}$/"; break; case 'tint': $pattern = "/^[0-9]{{$lenMin}, {$lenMax}}$/"; break; } preg_match($pattern, $subject, $matches); // check results and return true or false } Quote Link to comment https://forums.phpfreaks.com/topic/267313-complex-syntax-and-preg_match/ Share on other sites More sharing options...
Christian F. Posted August 19, 2012 Share Posted August 19, 2012 What does the finished pattern look like when you test it? As far as I can tell from what you've posted, it should work. Though it might be the space after the comma that creates problems for you. Not sure, as I've never tried with a space there. Quote Link to comment https://forums.phpfreaks.com/topic/267313-complex-syntax-and-preg_match/#findComment-1370736 Share on other sites More sharing options...
Drongo_III Posted August 19, 2012 Author Share Posted August 19, 2012 Thanks Christian - as ever Your spotting the space was indeed the issue! Works now. Quote Link to comment https://forums.phpfreaks.com/topic/267313-complex-syntax-and-preg_match/#findComment-1370741 Share on other sites More sharing options...
Christian F. Posted August 20, 2012 Share Posted August 20, 2012 You're welcome, and glad I could help. Nice to know that the issue was indeed the space, I shall keep that in mind henceforth. Quote Link to comment https://forums.phpfreaks.com/topic/267313-complex-syntax-and-preg_match/#findComment-1370826 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.