SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: preg_match()

  1. #1
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_match()

    Hi
    In the following code, could u give me the use of preg_match('/[a-z]/i', $password)

    Code:
    if(strlen($password) < 6 || !preg_match('/[a-z]/i', $password) ||
    	!preg_match('/[0-9]/', $password)) {
    	  echo "bad password";
    	}




    thanks,
    karthikanov24

  2. #2
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I assume that you want to know what exacltly preg_match function does here.

    PHP Code:
    !preg_match('/[a-z]/i' 
    It will check in your password,that you are not allowing excepts small letter and capital letters because "i" after the pattern delimiter indicates a case-insensitive search. nothing more than that.

    see this tutorial

    http://php.net/manual/en/function.preg-match.php


    Hope this will help you.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    preg_match('/[a-z]/i', $password)

    That checks if there is at least one letter.

    The entire if statement probably doesn't do what the programmer intended it to do.

  4. #4
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi
    why does the above code (if condition)does not do programmer's intention?

  5. #5
    SitePoint Enthusiast nrg_alpha's Avatar
    Join Date
    Dec 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can only assume from the OP that the requirements for the password is: 6 or more characters, and must only contain the characters a-zA-Z0-9?

    If so, then the following example will satisfy this:

    PHP Code:
    $arr = array('GJk79''GJk79a''dsaUOSH890-j''hdh76TRf_09''467aFg9''PASSWORD!'); // array of password examples
    foreach ($arr as $password) {
        echo (
    strlen($password) > && preg_match('#^[a-z0-9]+$#i'$password))? "$password is in the correct format!<br />\n" "$password is NOT in the correct format!<br />\n";

    Output:
    PHP Code:
    GJk79 is NOT in the correct format// under 6 characters long
    GJk79a is in the correct format!
    dsaUOSH890-j is NOT in the correct format// contains character other than [a-zA-Z0-9]
    hdh76TRf_09 is NOT in the correct format// contains character other than [a-zA-Z0-9]
    467aFg9 is in the correct format!
    PASSWORDis NOT in the correct format// contains character other than [a-zA-Z0-9] 

  6. #6
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi
    thanks to all!

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    better change it to '#^[a-z0-9]+$#Di'

    Without the D flag a trailing newline char is allowed

  8. #8
    SitePoint Enthusiast nrg_alpha's Avatar
    Join Date
    Dec 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    better change it to '#^[a-z0-9]+$#Di'
    Without the D flag a trailing newline char is allowed
    Thanks, I tend to forget about that modifier.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •