Check if a String Starts With a Substring in PHP

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

Table Of Contents

Method 1: Using substr()

To check if string starts with a given substring or not, we can fetch the first N characters from the given string, where N is the size of the given substring. Then we can compare this fetched substring with the given substring. If both are equal, then it means that the string starts with a given substring.

PHP provides a function substr(), and it accepts 3 parameters i.e. a string, an offset value and length of substring that need to be fetched. It returns a portion of string of given length from the given offset position.

So, to check if a String Starts With a Substring, we can pass the main string as the first argument and the offset position as zero and as the 3rd parameter we can pass the length of the substring i.e. N. It will return a substring containing first N characters from the string. Then we can compare it with a given substring if it is equal then it means that the string starts with a given substring.

We have created a separate function for it i.e.

function startsWith($str, $substring)
{
    return substr($str, 0, strlen($substring)) === $substring;
}

We can call this function to check if string $str starts with a $substring.

Let’s see the complete example,

<?php
/**
 * Checks if a string starts with a specific substring
 * using the substr() function.
 *
 * @param string $str The string to check.
 * @param string $substring The substring to check for at the beginning of the string.
 * @return bool True if the string starts with the substring, false otherwise.
 */
function startsWith($str, $substring)
{
    return substr($str, 0, strlen($substring)) === $substring;
}

$strValue = "Last Train";
$substring = "Last";

if (startsWith($strValue, $substring)) {
    echo "The string starts with the substring.";
} else {
    echo "The string does not start with the substring.";
}
?>

Output

The string starts with the substring.

Method 2: Using strpos() function

To check if string starts with a specific substring we can search for the substring in the main substring using the strpos() function. In this function, we will pass the string and the substring as two parameters. It will return the index position of the first occurrence of substring in the string. If it returns 0, then it means that the string starts with given substring.

Let’s see the complete example,

<?php
/**
 * Checks if a string starts with a specific substring
 * using the strpos() function.
 *
 * @param string $str The string to check.
 * @param string $substring The substring to check for at the beginning of the string.
 * @return bool True if the string starts with the substring, false otherwise.
 */
function startsWithSubString($str, $substring)
{
    return strpos($str, $substring) === 0;
}

$strValue = "Last Train";
$substring = "Last";

if (startsWithSubString($strValue, $substring)) {
    echo "The string starts with the substring.";
} else {
    echo "The string does not start with the substring.";
}
?>

Output

The string starts with the substring.

Summary

Today, we learned about two different ways to check if string starts with the substring 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