How Can I Check if String Starts with Specific Word or Substring?

Author — Nitish Kumar

When looking for a word, character or substring inside another string, it is very common to check if the main string starts with given substring. Basically, every technique that worked when looking for a subtring inside another string will also work when we need to check if the substring only occurs at the beginning. However, this is a more specific situation and we can perform the check more efficiently using slightly different methods.

On This Page

Using strpos() to Check if String Starts With Another Substring [Case Sensitive]

The PHP strpos() function returns the position of the first occurrence of a substring inside another string. It returns FALSE if the word or substring was not found. When you want to check if a substring occurs at the beginning of another string, you can simply compare the return value of strpos() to 0.

PHP

$sentence = "Some people that I meet are very funny.";
$word = "Some";

// Output — The word "Some" occurs at the start of given string.
if(strpos($sentence, $word) === 0) {
    echo 'The word "'.$word.'" occurs at the start of given string.';
}

You have to be extra careful when comparing the return value of strpos() with 0. This is because 0 will also evaluate to false with the == operator resulting in FALSE positives.

PHP

$sentence = "Some people that I meet are very funny.";
$word = "Apples";

// Output — The word "Apples" occurs at the start of string.
if(strpos($sentence, $word) == 0) {
    echo 'The word "'.$word.'" occurs at the start of given string.';
}

Using stripos() to Check if String Starts With Another Substring [Case Insensitive]

If you want the check for a substring at the beginning of another string, you can simply use the PHP stripos() function. The function operates exactly like strpos() above but does the check in a case insensitive manner.

PHP

$sentence = "Some people that I meet are very funny.";
$word = "some";

// Output — The word "some" occurs at the start of given string.
if(stripos($sentence, $word) === 0) {
    echo 'The word "'.$word.'" occurs at the start of given string.';
}

Alternatively, you can also use other functions like strtolower() or strtoupper() to change the case of all the strings involved before making the comparison. However, just using stripos() seems more straightforward.

We can combine the code from the first and second section to create a function that handles the case sensitive and case insensitive situation for us using an optional parameter.

PHP

function starts_with($haystack, $needle, $case_sensitive = true) {
    if ($case_sensitive) {
        return strpos($haystack, $needle) === 0;
    } else {
        return stripos($haystack, $needle) === 0;
    }
}

$sentence = "Some people that I meet are very funny.";
$word = "some";

// Output — The word "some" does not occur at the start of given string. [Case Sensitive]
if(starts_with($sentence, $word)) {
    echo 'The word "'.$word.'" occurs at the start of given string. [Case Sensitive]';
} else {
    echo 'The word "'.$word.'" does not occur at the start of given string. [Case Sensitive]';
}

// Output — The word "some" occurs at the start of given string. [Case Insensitive]
if(starts_with($sentence, $word, false)) {
    echo 'The word "'.$word.'" occurs at the start of given string. [Case Insensitive]';
} else {
    echo 'The word "'.$word.'" does not occur at the start of given string. [Case Insensitive]';
}

Using substr() to Check if String Starts With Another Substring

Another way to check if a string starts with given substring is to use the PHP substr() function. This function returns part of a string specified by the start and length parameters. We can simply compare the return value to our original substring.

One advantage of using substr() over strpos() is that it will not look for the needle in the whole haystack. This means that if there is little probability finding the substring at the beginning of the main string, substr() will outperform strpos().

PHP

$sentence = "Some people that I meet are very funny.";
$word = "Some";

$length = strlen($word);

// Output — The word "Some" occurs at the start of given string.
if (substr($sentence, 0, $length) === $word) {
    echo 'The word "'.$word.'" occurs at the start of given string.';
}

Using substr_compare() to Check if String Begins With Another Substring

Instead of using substr() to extract a part of the main string and then comparing it to your substring, you can also let PHP do all the work with the substr_compare() function. This function is used to perform comparison of two strings from an offset or start position, up to length characters. It returns an integer value < 0 if the main string from position offset is less than the other string, > 0 if it is greater than the other string, and 0 if they are equal.

You can also optionally make the comparison case-sensitive. The comparison would be case insensitive by default.

PHP

$sentence = "Some people that I meet are very funny.";
$word = "some";

// Output — The word "some" does not occur at the beginning of given string. [Case Insensitive]
if (substr_compare($sentence, $word, 0, $length) === 0) {
    echo 'The word "'.$word.'" occurs at the beginning of given string. [Case Insensitive]';
} else {
    echo 'The word "'.$word.'" does not occur at the beginning of given string. [Case Insensitive]';
}

// Output — The word "some" occurs at the beginning of given string. [Case Sensitive]
if (substr_compare($sentence, $word, 0, $length, true) === 0) {
    echo 'The word "'.$word.'" occurs at the beginning of given string. [Case Sensitive]';
} else {
    echo 'The word "'.$word.'" does not occur at the beginning of given string. [Case Sensitive]';
}

Quickly Check if a Specific Character Occurs at the Beginning of String

In certain situations, the substring that you are looking for might only be only character long. Using the comparison functions we have discussed so far will not be the most efficient way to make the check in these cases. When looking for a single character at the beginning of another string, you can simply access the first character of the main string using $string[0] and compare it with the given character.

We are not using any builtin functions in this case which means that this method will be much more quicker than all others.

PHP

$sentence = "!Hola!";
$character = "!";


// Output — The character "!" occurs at the beginning of given string.
if($sentence[0] === $character) {
    echo 'The character "'.$character.'" occurs at the beginning of given string.';
}

Quick Summary

Let’s recap everything we have covered in this tutorial.

  1. You can simply use strpos() to check if a string contains another word or substring at its beginning. All you have to do is compare the return value of strpos() with 0. If you are looking for a case insensitive match, you should do the comparison with the stripos() function.
  2. Another more efficient option is to use substr() to extract part of the main string and compare the result with the given word or substring. This way you don’t have to go through the whole haystack (or string) when searching for the needle (or substring).
  3. You can also let PHP handle the comparison and give you the final result by using the substr_compare() function. It will return 0 if the substring extracted from the main string and our original substring are equal.
  4. If you are only planning to check whether a string begins with a specific character, you can avoid using all these functions and simply access the character at first position using $string[0]. After that, you just have to compare the returned character with given character.

Let me know if there is anything that you would like me to clarify in this tutorial. Also, you are more than welcome to comment if you know other techniques to check if a string starts with another word, character or substring in PHP.

Rate this post —

Very PoorPoorAverageGoodExcellent (No Ratings Yet)
Loading...
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0%