Home  >  Article  >  Daily Programming  >  PHP check if string contains uppercase characters

PHP check if string contains uppercase characters

藏色散人
藏色散人Original
2019-01-25 14:40:356691browse

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.

PHP check if string contains uppercase characters

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(&#39;A&#39;) &&
            ord($str1[$sc]) <= ord(&#39;Z&#39;)) {
            return false;
        }
    }
    return true;
}
var_dump(is_str_lowercase(&#39;abc def ghi&#39;));
var_dump(is_str_lowercase(&#39;abc dEf ghi&#39;));

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn