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 :)
Printable View
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 :)
To find the string length, just simply use strlen() around the variable, as above. $len is equal to 18 in this example.PHP Code:<?php
$str = "contains UpperCASE";
$len = strlen($str);
$pattern = preg_match('/[A-Z]+/', $str);
if($pattern)
echo 'Uppercase Detected';
?>
I'm terrible at regex, but the above pattern seems to do the job.
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.
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.
If that, then you can confirm it in this way:
if ($p!=strtolower($p)&&$p!=strtoupper($p)) { ...complex... }
^umm... ?
do you mean?PHP Code:<?php
$str = 'UPPERCASE & lowercase';
if(preg_match('/[A-Z]+/', $str) && preg_match('/[a-z]+/', $str)){ /* all is well */ }
?>
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.)
Thanks, for all the information.
The aim is to check if word is without uppercases :D
Without uppercases?
Do this:
if ($p==strtolower($p)) { ...it's ok... }
Or, you can just do this:
$p = strtolower($p);
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.
It is done with this one... can someone take a look to this?? HERE
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.
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:PHP Code:preg_match('/(?=.*[A-Z])(?=.*[a-z])/', $str)