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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.