Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: PHP detect uppercase and length of the word

  1. #1
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default PHP detect uppercase and length of the word

    Hi,

    How I can detect the length of variable value?
    Also I want to check the same variable for Uppercase letters. If they are - script tells me.

    Thanks in Advance
    Last edited by auriaks; 03-27-2010 at 12:07 PM.

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    PHP Code:
    <?php
    $str 
    "contains UpperCASE";

    $len strlen($str);

    $pattern preg_match('/[A-Z]+/'$str);

    if(
    $pattern)
        echo 
    'Uppercase Detected';

    ?>
    To find the string length, just simply use strlen() around the variable, as above. $len is equal to 18 in this example.

    I'm terrible at regex, but the above pattern seems to do the job.

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I'm not sure what your purpose is, but you can also use strtolower() or strtoupper() to convert to all upper/lowercase and then you don't have to worry about inconsistencies if you are checking for that reason.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Quote Originally Posted by djr33 View Post
    I'm not sure what your purpose is..
    Always assume it's malicious!
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  5. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Almost always when I see a request for this it is to confirm whether a complex password was submitted; one that has upper and lowercase letters and numbers.
    To choose the lesser of two evils is still to choose evil. My personal site

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    If that, then you can confirm it in this way:
    if ($p!=strtolower($p)&&$p!=strtoupper($p)) { ...complex... }
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    ^umm... ?

    do you mean
    PHP Code:
    <?php
    $str 
    'UPPERCASE & lowercase';
    if(
    preg_match('/[A-Z]+/'$str) && preg_match('/[a-z]+/'$str)){ /* all is well */ }
    ?>
    ?

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    If the goal is to see if there are BOTH lowercase and uppercase, then using the two functions in my post above will work-- basically you're checking that it's already not all lowercase and not all uppercase.

    What you're doing could never return true, I believe. (Though I'm no good with regex either.)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    Thanks, for all the information.

    The aim is to check if word is without uppercases

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Without uppercases?
    Do this:
    if ($p==strtolower($p)) { ...it's ok... }

    Or, you can just do this:
    $p = strtolower($p);
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •