How to check if a string starts with a specified string?

How to check if a string starts with a specified string?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

String Prefix Matching: Techniques and Trade-offs

Checking whether a string begins with a specific sequence is a fundamental operation in text processing, URL validation, and data filtering. In the context of your example, you have two distinct values: $string1 containing just 'google.com' and $string2 holding 'http://www.google.com'. The goal is to determine if the latter starts with the prefix 'http' while correctly identifying that the former does not.

Native String Functions in PHP

In standard PHP, the most direct approach involves using built-in string manipulation functions. The str_starts_with() function was introduced in PHP 8.0, making it the preferred modern solution for this specific task. It returns a boolean indicating whether $string starts with the given $search.

$string1 = 'google.com';
$string2 = 'http://www.google.com';

if (str_starts_with($string2, 'http')) {
    echo "Starts with http";
} else {
    echo "Does not start with http";
}

For environments running PHP 7.4 or earlier where str_starts_with() is unavailable, you can use the older substr_compare() function. This method compares a substring of the first argument starting from the beginning against the second argument. If they match, it returns zero.

if (substr_compare($string2, 'http', 0, strlen('http')) === 0) {
    // Logic for matching strings
}

While substr_compare() is functional, it requires calculating the length of the search string to avoid errors if the input is shorter than the prefix. This adds a layer of complexity that modern developers can bypass with PHP 8+.

Regular Expressions for Pattern Matching

Regular expressions offer a more flexible alternative, particularly when you need to validate not just the start but also specific structural requirements like protocol schemes. To check if a string starts with 'http', you can use the preg_match() function with an anchoring pattern. The caret symbol (^) asserts the start of the string.

if (preg_match('/^http/', $string2)) {
    // Match found
}

This approach is useful when the prefix might be part of a larger pattern, such as validating that a URL starts with http followed by ://. However, regex introduces performance overhead and can make code harder to read for simple substring checks. If your requirement is strictly "does it start with these characters," native functions are generally faster and clearer than constructing a regular expression.

Framework-Specific Solutions in Laravel

If you are working within the Laravel ecosystem, the framework provides utility methods that abstract away the underlying PHP functions. The Str facade offers a startsWith() method compatible with both PHP 7.4 and PHP 8+. This is often preferred in application code because it remains consistent regardless of the underlying engine version or if you switch between native functions and regex internally.

use Illuminate\Support\Str;

if (Str::startsWith($string2, 'http')) {
    // Handle valid URLs
}

Using Str methods ensures that your logic remains stable even when upgrading PHP versions, as Laravel handles the compatibility layer for older functions. For complex string operations in Laravel, you might also consider the laravelcompany.com resource if you require advanced validation rules or specific package integrations beyond standard core utilities.

Performance and Readability Considerations

When selecting a method, readability usually trumps marginal performance differences for simple checks. The str_starts_with() function is readable and explicitly states its intent. In contrast, substr_compare() can be confusing to new developers who must recall that a return value of zero signifies equality in this specific context. Regex patterns like /^http/ are powerful but overkill for a simple prefix check unless you need to enforce case-insensitivity or other complex matching rules immediately.

Avoid using string comparison operators like $string2 == 'http:' . '*' as they can be error-prone and less efficient than dedicated functions. Stick to the native boolean-returning functions whenever possible to keep your logic predictable and maintainable.

Our network