Jump to content

Regular Expressions


porzant

Recommended Posts

<?php// The "i" after the pattern delimiter indicates a case-insensitive searchif (preg_match("/^([0-9]{1}|[0-9]{2})-[a-z]{3}, [0-9]{2}:[0-9]{2} GMT$/i", "01-Jun, 13:30 GMT")) {if(preg_match("/[A-Z0-9 _-]{3,12}/i", "abcdefghijklm")){    echo "A match was found.";} }else {    echo "A match was not found.";}?> 

First section works fine. On the second section, {3,12} means it should match those charactes for a minimum of 3 and a maximum of 12 times doesn't it? When there are 13 characters, it still returns the match as true. Anyone know why?

Link to comment
Share on other sites

The thing is that that reg.exp (/[A-Z0-9 _-]{3,12}/i) matches a substring/section of the string and not nessassary the whole string. (/abc/ matches "abc", "aabc", "123abc456" etc.). To make it to only match the whole string you need to add ^ (matches the beginning of the string) and $ (matches the end of the string) (just as you have in the first reg.exp. The regexp would the look like this:/^[A-Z0-9 _-]{3,12}$/iOr in the code:

<?php// The "i" after the pattern delimiter indicates a case-insensitive searchif (preg_match("/^([0-9]{1}|[0-9]{2})-[a-z]{3}, [0-9]{2}:[0-9]{2} GMT$/i", "01-Jun, 13:30 GMT")) {   if(preg_match("/^[A-Z0-9 _-]{3,12}$/i", "abcdefghijklm")) {	 echo "A match was found.";   }} else {	echo "A match was not found.";}?>

Good Luck and Don't Panic! ;?)

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...