In this PHP tutorial, you shall learn how to check if given string starts with a lowercase alphabet using preg_match() function, with example programs.

PHP – Check if String Starts with Lowercase

To check if string starts with lowercase alphabet, perform regular expression match if the first character is lowercase or not. Provide the regular expression that matches a string starts with lowercase, and the string as argument to preg_match(). The function shall return true if the string starts with lowercase alphabet, else it returns false.

Examples

ADVERTISEMENT

1. Check if given string starts with a lowercase alphabet

In this example we will use preg_match() to perform a regular expression match.

The regular expression to check if the first character in given string is lowercase alphabet or not, is

'~^\p{Ll}~u'

We will use this expressing with preg_match() built-in PHP function, to check if the string starts with lowercase. The syntax to use preg_match() for this use case is

preg_match('~^\p{Ll}~u', $string)

The above function call returns true if the string starts with lowercase alphabet, else it returns false.

In the following program, we will take a string “Hello World”, and check if this string starts with lowercase alphabet or not using preg_match().

PHP Program

<?php
$string = "hello world!";
if ( preg_match('~^\p{Ll}~u', $string) ) {
    echo "\"{$string}\" starts with lowercase.";
} else {
    echo "\"{$string}\" does not start with lowercase.";
}
?>

Output

PHP - Check if String Starts with Lowercase

Conclusion

In this PHP Tutorial, we learned how to check if a string starts with an lowercase alphabet, using PHP built-in function preg_match().