View Full Version : Resolved PHP detect uppercase and length of the word
auriaks
03-26-2010, 09:45 PM
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 :)
Schmoopy
03-26-2010, 10:05 PM
<?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.
djr33
03-27-2010, 12:31 AM
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.
BLiZZaRD
03-27-2010, 12:52 AM
I'm not sure what your purpose is..
Always assume it's malicious! :D
james438
03-27-2010, 02:18 AM
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.
djr33
03-27-2010, 02:50 AM
If that, then you can confirm it in this way:
if ($p!=strtolower($p)&&$p!=strtoupper($p)) { ...complex... }
^umm... ?
do you mean
<?php
$str = 'UPPERCASE & lowercase';
if(preg_match('/[A-Z]+/', $str) && preg_match('/[a-z]+/', $str)){ /* all is well */ }
?>?
djr33
03-27-2010, 05:16 AM
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.)
auriaks
03-27-2010, 07:56 AM
Thanks, for all the information.
The aim is to check if word is without uppercases :D
djr33
03-27-2010, 08:33 AM
Without uppercases?
Do this:
if ($p==strtolower($p)) { ...it's ok... }
Or, you can just do this:
$p = strtolower($p);
james438
03-27-2010, 11:49 AM
If ever you can script without using PCRE (Perl Compatible Regular Expressions) you should do so since PCRE uses a lot of processor resources. As I recall, Perl has an engine specifically designed for regex and is not processor heavy.
auriaks
03-27-2010, 12:08 PM
It is done with this one... can someone take a look to this?? HERE (http://www.dynamicdrive.com/forums/showthread.php?t=53535)
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.)
I see what you're doing now - yes, you're right. I was misreading the syntax.
I think my solution would work as well, though I haven't tried it. I'll let you know.
djr33
03-27-2010, 11:10 PM
Ah, and I think I was reading yours wrong.
Does the function check for ANY matches or a complete match from the whole string?
You're saying "exists [uppercase] and exists [lowercase]", not "[whole string is upper] and [whole string is lower]".
I read it the second way when I first saw your post.
In other words, preg_match returns TRUE upon finding a match, rather than returning FALSE upon finding a non-match.
Yes, that would work, but personally I'd prefer the more "human readable" string functions (when they work, of course-- sometimes regex is needed/easier).
Exactly. [A-Z] is the character range we're searching for, and + means "one or more of".
Using functions might be quicker, since regex can be more process-intensive, but with the (likely small) size of our $string, I don't think it would be an issue. I think it would improve the speed of the regex if I combined them into a single pattern, too, instead of checking twice.
djr33
03-28-2010, 07:29 AM
I'm not sure you would be able to combine them together in the sense that you are checking if (1) and if (2), not if (1/2).
My motivation for using string functions is simply that they are easier (for me). Regex likely is a bit slower, but using it can be more standardized if you are doing a lot of checks for lots of different patterns and it can also do a lot that string functions can't.
this works. true for UPPER and lowercase, but not UPPER or lowercase:
preg_match('/(?=.*[A-Z])(?=.*[a-z])/', $str)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.