Preg_Match regular problem expression

having problem with Preg_match successfully matching

Page 1 of 1

4 Replies - 7793 Views - Last Post: 04 August 2008 - 05:38 AM Rate Topic: -----

#1 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 566
  • Joined: 08-May 08

Preg_Match regular problem expression

Post icon  Posted 03 August 2008 - 08:37 AM

Hello all,
Been trying to debug a problem with my form submission and found it to be located in the preg_match function in PHP. Now i will confess i did not create this regular expression by myself....copied it from somewhere else...I understand regular expression but not enough to confuse others by making a statement.

Here is the problem. I have put in some debugging tracer echo's in the code for debugging....as well it will help you follow the problem up to the preg_match function.

Debugging that works:

Quote

$pwd: Passw0rd
$unique: 7
$difference: 0.875
$result: 1
$ps: 1
$valid: 1
$vpwd: Passw0rd
$ps2: 1
$valid: 1
Form filled successfully!


Debugging that fails (with similar password):

Quote

$pwd: Siycnx858
$unique: 8
$difference: 0.88888888888889
$result: 0
$ps: 0
$valid:
$vpwd: Siycnx858
$ps2: 1
$valid:


The arrows mark the offending function and its regular expression. From what it looks like to me that the password should have the format Letter-Numbers-Letters in order for it to be a valid password. I would like for it be a combination of captial, numeric, and special characters. Again, i am no regex coder, and right now, that is a little too involved for me to learn the intracacies of that coding language. I unfortunately would appreciate someone looking at the regex and seeing what the problem is and then please posting a valid regex to place in there.

function checkPassword($password){
	   $length = strlen($password);
	   if($length<8){
	     return false;
	   }
	   $unique=strlen(count_chars($password, 3));
	   echo '$unique: ' . "$unique";
	   $difference = $unique/$length;
	   echo '$difference: ' . "$difference";
	   if($difference < .60){
	     return false;
	   }
------>	   $result = preg_match ("/[A-z]+[0-9]+[A-z]+/", $password); <----------
	   echo '$result: ' . "$result";
	   return $result;
	}



Is This A Good Question/Topic? 0
  • +

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5078
  • View blog
  • Posts: 13,707
  • Joined: 18-April 07

Re: Preg_Match regular problem expression

Posted 03 August 2008 - 08:56 AM

Well you are right, the regular expression is determining if the pattern of Letter (one or more) is followed by a number (one or more) then followed by a letter (one or more). In the first password you have w followed by 0 followed by r which meets this pattern and so it is valid.

The second password though has a letter, followed by a number, but not followed by more letters so it is invalid.

If you want to check for passwords that have just a series of numbers, letters and special characters you can try something like...

$result = preg_match ("/^[a-zA-Z0-9%&@]{5,8}$/", $password);



This essentially says "If the WHOLE password contains letters from a-z or capitals, 0-9 (digits) and the special characters %&@ and is at least 5 to 8 characters, then it is good.

If you want to add more special chars you can just put them in between the square brackets but keep in mind that some characters are special in regular expressions so they will need to be escaped using a backslash.

Hopefully this is what you were looking for. If not, tell us the exact pattern you want. Thanks.

"At DIC we be regular expression throwing code ninjas...we throw expressions so regularly that we have become masters at it." :snap:
Was This Post Helpful? 1

#4 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 566
  • Joined: 08-May 08

Re: Preg_Match regular problem expression

Posted 03 August 2008 - 09:05 AM

:bow: master regex ninja :) Yeah that looks good....ty sire.
Was This Post Helpful? 0
  • +
  • -

#5 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 566
  • Joined: 08-May 08

Re: Preg_Match regular problem expression

Posted 03 August 2008 - 09:11 AM

I tried a password that was in this form AAaaaa000 and it failed....against the regex you supplied....trying to figure out why?
Was This Post Helpful? 0
  • +
  • -

#6 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 566
  • Joined: 08-May 08

Re: Preg_Match regular problem expression

Posted 04 August 2008 - 05:38 AM

Sorry i will test against a password that meets the length of the regex.....i can count wonderfully sometimes....
Was This Post Helpful? 1

Page 1 of 1