Check if a String is ALL CAPS in PHP
Check whether strtoupper($str) == $str
To handle non-ascii use:
mb_strtoupper($str, 'utf-8') == $str
How to check if letter is upper or lower in PHP?
It is my opinion that making a preg_
call is the most direct, concise, and reliable call versus the other posted solutions here.
echo preg_match('~^\p{Lu}~u', $string) ? 'upper' : 'lower';
My pattern breakdown:~ # starting pattern delimiter
^ #match from the start of the input string
\p{Lu} #match exactly one uppercase letter (unicode safe)
~ #ending pattern delimiter
u #enable unicode matching
Please take notice when ctype_
and < 'a'
fail with this battery of tests.Code: (Demo)
$tests = ['âa', 'Bbbbb', 'Éé', 'iou', 'Δδ'];
foreach ($tests as $test) {
echo "\n{$test}:";
echo "\n\tPREG: " , preg_match('~^\p{Lu}~u', $test) ? 'upper' : 'lower';
echo "\n\tCTYPE: " , ctype_upper(mb_substr($test, 0, 1)) ? 'upper' : 'lower';
echo "\n\t< a: " , mb_substr($test, 0, 1) < 'a' ? 'upper' : 'lower';
$chr = mb_substr ($test, 0, 1, "UTF-8");
echo "\n\tMB: " , mb_strtoupper($chr, "UTF-8") == $chr ? 'upper' : 'lower';
}
Output:âa:
PREG: lower
CTYPE: lower
< a: lower
MB: lower
Bbbbb:
PREG: upper
CTYPE: upper
< a: upper
MB: upper
Éé: <-- trouble
PREG: upper
CTYPE: lower <-- uh oh
< a: lower <-- uh oh
MB: upper
iou:
PREG: lower
CTYPE: lower
< a: lower
MB: lower
Δδ: <-- extended beyond question scope
PREG: upper <-- still holding up
CTYPE: lower
< a: lower
MB: upper <-- still holding up
If anyone needs to differentiate between uppercase letters, lowercase letters, and non-letters see this post.It may be extending the scope of this question too far, but if your input characters are especially squirrelly (they might not exist in a category that
Lu
can handle), you may want to check if the first character has case variants:\p{L&} or \p{Cased_Letter}: a letter that exists in lowercase and uppercase variants (combination of Ll, Lu and Lt).
- Source: https://www.regular-expressions.info/unicode.html
SMALL
variants, you can add that extra range to the pattern if necessary.https://www.fileformat.info/info/unicode/category/Nl/list.htm
Code: (Demo)
echo preg_match('~^[\p{Lu}\x{2160}-\x{216F}]~u', $test) ? 'upper' : 'not upper';
How to detect if string contains 1 uppercase letter in PHP
Some regular expression should be able to the work, you can use preg_match
and [A-Z]
if(preg_match('/[A-Z]/', $domain)){
// There is at least one upper
}
How to Check If a String Contains a Specific Word with uppercase and lowercase in PHP
try converting both to the same case:
strpos(strtoupper($company_name), 'SDN');
You could also use preg_match
preg_match('/SDN/i', $company_name);
https://www.php.net/manual/en/function.preg-match.php PHP uppercase letter in String
You can try this:
if (strtolower($string) != $string) {
echo 'You have uppercase in your string';
} else {
echo 'You have no uppercase in your string';
}
This checks if the converted string to lowercase is equal to the original string. Hope this helps... Identify and get capitalized words in a string in php
You can use a regular expression for it:
preg_match_all('/\b([A-Z]+)\b/', $str, $matches);
To match the lowercase words, use this:preg_match_all('/\b([a-z]+)\b/', $str, $matches2);
Then you can use functions such as implode()
to build the strings. How to check if input value begins with an uppercase/ or if it has lowercases/ or if it's all uppercase in PHP?
To check the first letter with upper case try below
preg_match("/^[A-Z]/", 'Bamboozler');// This will return true if first letter is upper case
To check all the character of a string lowercase, you can use
ctype_lower()
functionctype_lower('bamboozler');// This will return true if all the character are lower cases.
To check all the character of a string uppercase, you can use
ctype_upper()
functionctype_upper('BOOM');// This will return true if all the character is upper cases.
PHP: How to convert a string that contains upper case characters
You can use preg_replace
to replace any instance of a lowercase letter followed with an uppercase with your lower-dash-lower variant:
$dashedName = preg_replace('/([^A-Z-])([A-Z])/', '$1-$2', $className);
Then followed by a strtolower()
to take care of any remaining uppercase letters:return strtolower($dashedName);
The full function here:function camel2dashed($className) {
return strtolower(preg_replace('/([^A-Z-])([A-Z])/', '$1-$2', $className));
}
To explain the regular expression used:
/ Opening delimiter
( Start Capture Group 1
[^A-Z-] Character Class: Any character NOT an uppercase letter and not a dash
) End Capture Group 1
( Start Capture Group 2
[A-Z] Character Class: Any uppercase letter
) End Capture Group 2
/ Closing delimiter
As for the replacement string$1 Insert Capture Group 1
- Literal: dash
$2 Insert Capture Group 2
Related Topics
PHP Code Inside a Laravel 5 Blade Template
Yii Framework 2.0 Login with User Database
Order Properties Should Not Be Accessed Directly - Woocommerce 3.0
Converting Nsarray -> JSON -> Nsdata -> PHP Server ->JSON Representation
Php: How to See "Invisible" Characters Like \N
MySQL Insert ....On Duplicate Update - Adds One to the Autoincrement
Getting Hour and Minute in PHP
Codeigniter Active Record Not Equal
Iconv_Strlen Function Causing Execution Timeout, Running on Mamp
File_Get_Contents() Failed to Open Stream:
Exploding by Array of Delimiters
Get the Get Variables from a Url String
Use PHP to Check If Page Was Accessed with Ssl
Remove "(Optional)" Text from Checkout Fields in Woocommerce 3.4+
Codeigniter 3 - Access Session from Outside Codeigniter Installation