Check if String Starts With an Uppercase in PHP

This tutorial will discuss about unique ways to check if string starts with an uppercase in php.

Table Of Contents

Method 1: Using ctype_upper() function

To check if string starts with an uppercase character we will use the ctype_upper() function. It accepts a string as an argument and returns true if all the characters in the string are uppercase characters.

Now to check if the first character is uppercase or capital, we will fetch the first character of the string using the index 0 in the subscript operator i.e. $str[0]. It will return the first character of the string, then we will pass into the ctype_upper() function and it will return true if the character is uppercase.

We have created a separate function that accepts string as an argument and returns true if the first character of the string is uppercase character i.e.

function startsWithUppercase($str)
{
    return ctype_upper($str[0]);
}

It accepts a string as an argument, and returns true if the string starts with a capital letter.

Let’s see the complete example,

<?php
/**
 * Checks if a string starts with an uppercase letter using ctype_upper().
 *
 * @param string $str The string to check.
 * @return bool True if the string starts with an uppercase letter, false otherwise.
 */
function startsWithUppercase($str)
{
    return ctype_upper($str[0]);
}

$strValue = "Last Train";

if (startsWithUppercase($strValue)) {
    echo "The string starts with an uppercase letter.";
} else {
    echo "The string does not start with an uppercase letter.";
}
?>

Output

The string starts with an uppercase letter.

Method 2: Using Regex

PHP provides a function preg_match(), and it accepts a regex pattern as first parameter and a string as the second parameter. It returns 1, if the given regex pattern matches the given string.

Now, to check if a string starts with an uppercase letter, we can use the preg_match() function. For this, we will pass the regex pattern “/^[A-Z]/”, as the first parameter and the given string as the second parameter. If it returns 1 it means that the string starts with an upper case letter.

We have created a separate function for this like this it will accept a string as an argument and returns true if the string starts within a case letter i.e.

function startsWithUppercaseLetter($str)
{
    return preg_match('/^[A-Z]/', $str) === 1;
}

It accepts a string as an argument, and returns true if the string starts with a capital letter.

Let’s see the complete example,

<?php
/**
 * Checks if a string starts with an uppercase letter using regular expressions (preg_match()).
 *
 * @param string $str The string to check.
 * @return bool True if the string starts with an uppercase letter, false otherwise.
 */
function startsWithUppercaseLetter($str)
{
    return preg_match('/^[A-Z]/', $str) === 1;
}

$strValue = "Last Train";

if (startsWithUppercaseLetter($strValue)) {
    echo "The string starts with an uppercase letter.";
} else {
    echo "The string does not start with an uppercase letter.";
}
?>

Output

The string starts with an uppercase letter.

Summary

Today, we learned about two different ways to check if string starts within upper case letter in PHP.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top