Jump to content

Archived

This topic is now archived and is closed to further replies.

Mateobus

String Question

Recommended Posts

I just want to do a Boolean check of a string that returns true if the last two characters of the string are upper case and false otherwise. Thanks.

Share this post


Link to post
Share on other sites
You can do it with preg_match() too, but this is probably one of the easiest ways:
[code]
$text = 'abCD';

// Make sure the string is always 2 characters or more otherwise substr() returns false
if (ctype_upper(substr($text, -2)) {
       echo "The string $text last 2 characters consists of uppercase letters.\n";
   } else {
       echo "The string $text last 2 characters do not consist of uppercase letters.\n";
   }
[/code]

Share this post


Link to post
Share on other sites
<?php
$blah = "abcdef";

$test = substr($blah, -2);
if (ctype_upper($test)) {
echo "letters are uppercase";
}
else { echo "letters are not uppercase"; }
?>

edit: gah! you beat me :(

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.