Jump to content


Photo

simple regex problem


  • Please log in to reply
4 replies to this topic

#1 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 29 September 2006 - 05:39 PM


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

Help?


$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.");
		} 



#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 29 September 2006 - 06:15 PM


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

Help?


$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.");
		} 


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:
<?php
$min = 6;
$max = 25;
$pattern = "|^[a-z]\{$min,$max\}$|i";
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 29 September 2006 - 08:34 PM

i tried what you said but it still doesnt work any other ideas as to how i can accomplish the same thing

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 30 September 2006 - 01:10 AM

i tried what you said but it still doesnt work any other ideas as to how i can accomplish the same thing


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
<?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";
}

?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 30 September 2006 - 06:50 PM

sorry, im just checking back in.

I solved it using the following 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.");
		} 

?>

But from what i see yours would work also

Thanks so much for your help u got me lookin in the right direction.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users