Jump to content

simple regex problem


oracle259

Recommended Posts


I have the following code which should work but for some reason it doesnt

Help?

[code]

$max = '25';
$min = '6';

$pattern = "/^[a-zA-Z]{$min,$max}$/";
    $isletter = preg_match($pattern, $username);
          if (!$isletter) {
die ("<b>Program Error:</b>&nbsp;Username should contain only letters and must be between $min to $max characters in lenght.");
}

[/code]
Link to comment
https://forums.phpfreaks.com/topic/22519-simple-regex-problem/
Share on other sites

[quote author=oracle259 link=topic=109990.msg443832#msg443832 date=1159551553]

I have the following code which should work but for some reason it doesnt

Help?

[code]

$max = '25';
$min = '6';

$pattern = "/^[a-zA-Z]{$min,$max}$/";
    $isletter = preg_match($pattern, $username);
          if (!$isletter) {
die ("<b>Program Error:</b>&nbsp;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]
Link to comment
https://forums.phpfreaks.com/topic/22519-simple-regex-problem/#findComment-101018
Share on other sites

[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]
Link to comment
https://forums.phpfreaks.com/topic/22519-simple-regex-problem/#findComment-101217
Share on other sites

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>&nbsp;Username should contain only letters and must be between $min to $max characters in lenght.");
}

?>
[/code]

But from what i see yours would work also

Thanks so much for your help u got me lookin in the right direction.
Link to comment
https://forums.phpfreaks.com/topic/22519-simple-regex-problem/#findComment-101521
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.