oracle259 Posted September 29, 2006 Share Posted September 29, 2006 I have the following code which should work but for some reason it doesntHelp?[code]$max = '25';$min = '6'; $pattern = "/^[a-zA-Z]{$min,$max}$/"; $isletter = preg_match($pattern, $username); if (!$isletter) { die ("<b>Program Error:</b> Username should contain only letters and must be between $min to $max characters in lenght."); } [/code] Quote Link to comment Share on other sites More sharing options...
obsidian Posted September 29, 2006 Share Posted September 29, 2006 [quote author=oracle259 link=topic=109990.msg443832#msg443832 date=1159551553]I have the following code which should work but for some reason it doesntHelp?[code]$max = '25';$min = '6'; $pattern = "/^[a-zA-Z]{$min,$max}$/"; $isletter = preg_match($pattern, $username); if (!$isletter) { die ("<b>Program Error:</b> Username should contain only letters and must be between $min to $max characters in lenght."); } [/code][/quote]when you use brackets within double quotes, PHP tries to interpret the variable in between, so it is actually dying on the comma between the variables. escape the braces, and you should be good:[code]<?php$min = 6;$max = 25;$pattern = "|^[a-z]\{$min,$max\}$|i";?>[/code] Quote Link to comment Share on other sites More sharing options...
oracle259 Posted September 29, 2006 Author Share Posted September 29, 2006 i tried what you said but it still doesnt work any other ideas as to how i can accomplish the same thing Quote Link to comment Share on other sites More sharing options...
obsidian Posted September 30, 2006 Share Posted September 30, 2006 [quote author=oracle259 link=topic=109990.msg443958#msg443958 date=1159562054]i tried what you said but it still doesnt work any other ideas as to how i can accomplish the same thing[/quote]why don't you show your updated code. i just tested it myself, and it works fine ??? actually, slight alteration in code... something with how the regex engine interprets the brackets, quotes and dollar signs... just jump out of your quotes and do it like this, and you'll be fine[code]<?php$min = 6;$max = 25;$pattern = "|^[a-z]{" . $min . "," . $max . "}$|i";if (isset($_POST['name'])) { if (preg_match($pattern, $_POST['name'])) echo "<p class=\"success\">Passed</p>\n"; else echo "<p class=\"error\">Failed</p>\n";}?>[/code] Quote Link to comment Share on other sites More sharing options...
oracle259 Posted September 30, 2006 Author Share Posted September 30, 2006 sorry, im just checking back in.I solved it using the following code[code]<?php$max = '25';$min = '6'; $pattern = "/^[a-zA-Z]"."{".$min.",".$max."}$/"; $isletter = preg_match($pattern, $username); if (!$isletter) { die ("<b>Program Error:</b> Username should contain only letters and must be between $min to $max characters in lenght."); } ?>[/code]But from what i see yours would work alsoThanks so much for your help u got me lookin in the right direction. Quote Link to comment 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.