How To Check If A String Starts With a Specified String In PHP?

Published October 19, 2024

Checking String Prefixes in PHP

When working with strings in PHP, you often need to find out if a string starts with a specific sequence of characters. This task is common in programming scenarios, such as validating input, parsing data, or filtering text.

PHP Solutions for String Prefix Checking

Using the str_starts_with() Function (PHP 8+)

PHP 8 introduced the str_starts_with() function for prefix checking. This function takes two parameters: the string to check and the prefix to look for. It returns a boolean value indicating if the string starts with the prefix.

Here's how to use str_starts_with():

$string = "http://www.example.com";
$prefix = "http";

if (str_starts_with($string, $prefix)) {
    echo "The string starts with 'http'";
} else {
    echo "The string does not start with 'http'";
}

Tip: Case-Sensitive Prefix Checking

Remember that str_starts_with() is case-sensitive. If you need case-insensitive matching, convert both the string and prefix to lowercase before comparison:

$string = "HTTP://www.example.com";
$prefix = "http";

if (str_starts_with(strtolower($string), strtolower($prefix))) {
    echo "The string starts with 'http' (case-insensitive)";
}

Using substr() for Prefix Comparison

For PHP versions before 8, you can use the substr() function to check string prefixes. This method extracts the beginning of the string and compares it to the prefix.

Here's an example:

$string = "http://www.example.com";
$prefix = "http";

if (substr($string, 0, strlen($prefix)) === $prefix) {
    echo "The string starts with 'http'";
} else {
    echo "The string does not start with 'http'";
}

This approach works with prefixes of different lengths:

$string = "https://www.example.com";
$prefix = "https://";

if (substr($string, 0, strlen($prefix)) === $prefix) {
    echo "The string starts with 'https://'";
} else {
    echo "The string does not start with 'https://'";
}

Using strpos() for Prefix Detection

Another method to check for string prefixes is using the strpos() function. This function finds the position of the first occurrence of a substring in a string. If the substring is at the beginning, it returns 0.

Here's how to use strpos() for prefix checking:

$string = "http://www.example.com";
$prefix = "http";

if (strpos($string, $prefix) === 0) {
    echo "The string starts with 'http'";
} else {
    echo "The string does not start with 'http'";
}

Use the strict comparison (===) with 0, as strpos() returns false if the substring is not found, which is different from 0 in PHP.

Example: Multiple Prefix Checking

You can use strpos() to check for multiple prefixes efficiently:

$string = "ftp://example.com";
$prefixes = ["http", "https", "ftp"];

foreach ($prefixes as $prefix) {
    if (strpos($string, $prefix) === 0) {
        echo "The string starts with '$prefix'";
        break;
    }
}

Alternative Methods for String Prefix Verification

Regular Expressions with preg_match()

Regular expressions can check for string prefixes, especially with complex patterns. The preg_match() function in PHP uses regular expressions for prefix verification.

Here's how to use preg_match() for basic prefix checking:

$string = "http://www.example.com";
$pattern = "/^http/";

if (preg_match($pattern, $string)) {
    echo "The string starts with 'http'";
} else {
    echo "The string does not start with 'http'";
}

For complex patterns, regular expressions are useful:

$string = "123-456-7890";
$pattern = "/^[0-9]{3}-/";

if (preg_match($pattern, $string)) {
    echo "The string starts with three digits followed by a hyphen";
} else {
    "The string does not match the pattern";
}

Tip: Case-Insensitive Prefix Matching

To perform case-insensitive prefix matching with regular expressions, add the 'i' modifier at the end of the pattern:

$string = "HTTP://www.example.com";
$pattern = "/^http/i";

if (preg_match($pattern, $string)) {
    echo "The string starts with 'http' (case-insensitive)";
} else {
    echo "The string does not start with 'http'";
}

The strncmp() Function for Prefix Comparison

The strncmp() function compares the first n characters of two strings. It's useful for prefix checking as you can specify the number of characters to compare.

Here's how to use strncmp() for prefix verification:

$string = "http://www.example.com";
$prefix = "http";

if (strncmp($string, $prefix, strlen($prefix)) === 0) {
    echo "The string starts with 'http'";
} else {
    echo "The string does not start with 'http'";
}

strncmp() has some benefits:

  1. It's binary-safe, suitable for comparing strings with null bytes.
  2. It's faster than substr() for large strings, as it doesn't create a new string.

Here's an example comparing multiple prefixes:

$string = "https://www.example.com";
$prefixes = ["http://", "https://", "ftp://"];

foreach ($prefixes as $prefix) {
    if (strncmp($string, $prefix, strlen($prefix)) === 0) {
        echo "The string starts with '$prefix'";
        break;
    }
}

These methods give you options for string prefix verification, letting you pick the best approach for your needs.