Forum Moderators: coopster

Message Too Old, No Replies

Whats the best way to ?

         

George_W

8:02 pm on Feb 20, 2009 (gmt 0)

10+ Year Member



I need to find out if the text from a user input
is in capitals or lowercase so I can process the
informatiom wahts the best way using php ?
May thanks in advance George

IanKelley

8:27 pm on Feb 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if (preg_match('/^[A-Z0-9\s]+$/',$string)) { 
// string is uppercase
}
else {
// string is not uppercase
}

Note that the above assumes the user input will contain nothing but letters, numbers and whitespace. If it could contain special characters you will need to either filter them out ahead of time or include them in the match.

rocknbil

4:12 pm on Feb 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I may . . . :-)

if (preg_match('/^[A-Z0-9\s]+$/',$string)) {
// string is uppercase
}

<input type="text" value="123456">
or
<input type="text" value=" ">

would match.

Also ^ means "begins with" and "$" means ends with*, saying "if it begins AND ends with capitals, numbers, or a space," so

<input type="text" value="this IS MY string">
<input type="text" value="this IS MY 20th string">

would not match. Only strings like

<input type="text" value="This is string number 25">

would match. You might want, instead,

if (preg_match('/[A-Z]+/',$string))

or don't even check, just . . . fix it. :-)

$string = (preg_replace('/(.)+/',"\L$1",$string))

or

$string = strtolower($string);

This is a perl script (sorry folks, easier and faster for me, and with a few exceptions, treatment of regexps are the same) to demonstrate the difference, but you could do the same test in PHP.


#! /usr/bin/perl -w
$pattern1 = '^[A-Z0-9\s]+$';
$pattern2= '[A-Z]+';
%patterns = (
'numbers only' => '123456',
'spaces only' => ' ',
'mid-string caps' => 'this IS MY string',
'mid-string numbers' => 'this is my 20th string',
'begins with cap' => 'This is string beginning with cap',
'ends with number' => 'this is string number 25',
'Begins with cap, ends with number' => 'This is string number 25'
);
print "pattern $pattern1 \n";
foreach $v (keys %patterns) {
if ($patterns{$v} =~ /$pattern1/) {
print "$v matches: $patterns{$v}\n";
}
}
print "\npattern $pattern2 \n";
foreach $v (keys %patterns) {
if ($patterns{$v} =~ /$pattern2/) {
print "$v matches: $patterns{$v}\n";
}
}

Resulting output:


pattern ^[A-Z0-9\s]+$
numbers only matches: 123456
spaces only matches:


pattern [A-Z]+
mid-string caps matches: this IS MY string
begins with cap matches: This is string beginning with cap
Begins with cap, ends with number matches: This is string number 25

* ^ and $ are actually for lines that being and end matching the pattern, but they work in the above context and are used this way often so it's a perfectly valid regexp. An alternate would be to use a word boundary, /\b[A-Z]+\b/, but this may create complications with multiple words in this context - or be more accurate, depending on your task.

whoisgregg

7:27 pm on Feb 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, George_W!

I think the most straightforward way of telling all caps is a check like so:

if($string === strtoupper($string)){
// it's all uppercase
} else {
// there's at least one lowercase letter
}

You would still probably want to do checks for empty values, etc. but I think this may accomplish what you want. We could probably be even more helpful if we knew a bit more about what you were trying to ultimately accomplish. :)

IanKelley

9:13 pm on Feb 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Also ^ means "begins with" and "$" means ends with*, saying "if it begins AND ends with capitals, numbers, or a space," so

This is incorrect. ^ (when not in brackets) means beginning of line and $ means end of line.

In the case of an <input type="text"> field this will be the entire string. With an extra flag it could also apply to <textarea>

I took the original posters question to mean strings in all caps versus strings with lowercase in them. He wanted to tell them apart, not filter them. I made this assumption because mixed case strings are the rule as opposed to the exception.

So by including the begin and end lines we force the parser to only return a postive match if the string is ALL caps (with a free pass to numbers and spaces).

The following code shows it in action (hehe, you forced me to test it). As you can see a string that begins and ends with a capital does not match.

foreach (array('ALL CAPS','ALLCAPS','ALL 989 CAPS','SOme CapS','all lowercase') as $string) {
if (preg_match('/^[A-Z0-9\s]+$/',$string)) {
print "<p>$string is all caps</p>";
}
else {
print "<p>$string is not all caps</p>";
}
}

However whoisgregg's suggestion is even better, assuming you want to let special characters through. If you do not, the preg_solution is very slightly more efficient in terms of CPU time.

henry0

10:13 pm on Feb 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Out of curiosity, what do you expect or what do you wish receiving?
and if a user enters something like: aaaAAAaaAaaA?

wouldn't it be easier to assume that the input is wrong and upon your choice force to lower or upper

rocknbil

4:54 pm on Feb 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is incorrect. ^

I was generalizing, see footnote . . .

* ^ and $ are actually for lines that being and end matching the pattern...

He wanted to tell them apart, not filter them.

Ahh, once again, I was "somewhere else." Fixing and testing at the same time . . . guess it really depends on what you want to do.

George_W

1:05 pm on Feb 24, 2009 (gmt 0)

10+ Year Member



Sorry for the delay in replying to all your helpful replies
What Iam am trying to achive is
I produce vinyl lettering from a user input
There text can be anything from uppercase , lowercase
numbers etc.......

say the input was Private Parking
each charachter in that input will have a different width
so I need to know what characters are in the input so I can add all these different widths together to give a max length the line of lettering will be , based on height and font used ......

The font and Height I can do its nowing what each character is i.e upper case or lower case
abcdefg etc. ..................... George

penders

1:38 pm on Feb 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



...its nowing what each character is i.e upper case or lower case

By the sounds of it, it's not just whether a character is upper or lower case, but what that character actual is? ie. A, a, I, i, W, w could all be different I guess? Is that right?

George_W

2:14 pm on Feb 24, 2009 (gmt 0)

10+ Year Member



yes thats correct
could I convert them to unicode and use that code
to find out exactly what each characther is ?
George

penders

2:59 pm on Feb 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You don't need to resort to unicode unless you have to deal with a large variety of characters (ie. different languages) or perhaps your lookup table is based on unicode?

You can access the individual characters of a string by simply referencing the string like an array (starting at 0 for the first character) ie.

$mystring = 'Hello World'; 
echo $mystring[6];

Will output 'W'.