PHP checks whether the string contains uppercase characters, that is, whether the specified string is all lowercase. We can achieve this through for loops and strlen() and ord() functions.
Let’s take a look at specific code examples to introduce how PHP checks whether a string contains uppercase characters.
The code example is as follows:
<?php function is_str_lowercase($str1) { for ($sc = 0; $sc < strlen($str1); $sc++) { if (ord($str1[$sc]) >= ord('A') && ord($str1[$sc]) <= ord('Z')) { return false; } } return true; } var_dump(is_str_lowercase('abc def ghi')); var_dump(is_str_lowercase('abc dEf ghi'));
Here we create an is_str_lowercase method to determine whether the strings 'abc def ghi' and 'abc dEf ghi' are uppercase or all lowercase.
The output is as follows:
bool(true) bool(false)
Then returning true means that the string is all lowercase, returning false means that the string contains uppercase characters.
Required function:
ord() function Convert the first byte of the string to a value between 0-255
ord ( string $string ) : int
Parse string The first byte of the binary value is an unsigned integer type ranging from 0 to 255. If the string is a single-byte encoding such as ASCII, ISO-8859, Windows 1252, etc., it is equivalent to returning the position of the character in the character set encoding table. But please note that this function does not detect the encoding of the string, especially Unicode code points for multi-byte characters such as UTF-8 or UTF-16.
This function is the complementary function of chr() .
Parametersstring represents a character.
Return value: Return an integer value from 0 to 255.
strlen() function means getting the string length
strlen ( string $string ) : int
Returns the length of the given string string.
The parameter string represents the string whose length needs to be calculated.
Return value: If successful, the length of the string string is returned; if string is empty, 0 is returned.
This article is an introduction to the method of checking whether a string contains uppercase characters in PHP. It is simple and easy to understand. I hope it will be helpful to friends in need!
The above is the detailed content of PHP check if string contains uppercase characters. For more information, please follow other related articles on the PHP Chinese website!