Home > Article > Backend Development > How to determine whether a string is all lowercase in php
php determines whether the string is all lowercase. We can achieve this through PHP loop statements and the ord function. The ord function converts the first byte of the string to a value between 0-255.
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 function and test to determine the two strings "abc def ghi" and "abc dEf ghi" is all lowercase.
The judgment result is as follows:
#The ord function means that the first byte of the string is converted to a value between 0-255.
Syntax description:
int ord ( string $string )
The first byte of parsed string binary value is an unsigned integer type ranging from 0 to 255. ,
string represents a character. Returns an integer value 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 article is an introduction to the method of PHP to determine whether a string is all lowercase. It is also very simple. I hope it will be helpful to friends in need!
The above is the detailed content of How to determine whether a string is all lowercase in php. For more information, please follow other related articles on the PHP Chinese website!