Current Location: Home> Latest Articles> Check whether the string has certain words at the beginning: Stripos implementation method

Check whether the string has certain words at the beginning: Stripos implementation method

M66 2025-05-17

In PHP, the stripos function is a very useful tool to find where a string first appears in another string, and this lookup is case sensitive. Its syntax is as follows:

 stripos(string $haystack, string $needle, int $offset = 0): int|false
  • haystack : The target string to be found.

  • needle : The substring we want to find.

  • offset : optional parameter, start looking at this position.

Usually, stripos is used to check if a string contains another string, but if we want to check if a certain string is in the beginning of another string, we can do this using it.

1. Basic examples

First, let's look at a basic example, demonstrating how to use the stripos function to find the beginning of a string.

Suppose we have a string "Hello World" and we want to check if it starts with "Hello" :

 <?php
$haystack = "Hello World";
$needle = "Hello";

if (stripos($haystack, $needle) === 0) {
    echo "String with '{$needle}' beginning";
} else {
    echo "The string does not '{$needle}' beginning";
}
?>

In this example, stripos returns the first occurrence position. Since we are checking for the beginning of the string, we want it to return 0 , which means the target substring appears at the first position of the string. If the returned 0 , it means that the target string does appear at the beginning.

2. How to check the beginning vocabulary in URL

Now, suppose we need to check for a specific part of the URL. We can use a similar method to check the beginning of the URL. For example, check if a URL starts with "https://m66.net" . To demonstrate this, we replaced the URL domain with m66.net .

 <?php
$url = "https://m66.net/this-is-a-test-url";
$needle = "https://m66.net";

if (stripos($url, $needle) === 0) {
    echo "URL by '{$needle}' beginning";
} else {
    echo "URL 没有by '{$needle}' beginning";
}
?>

In this example, we use stripos to find https://m66.net in the URL string and make sure it appears at the beginning of the string. Since the URL starts at https://m66.net , we expect the function to return 0 , indicating that it does start with the specified part.

3. Things to note

  • Case sensitivity : The stripos function is case-insensitive, which means it can find the target vocabulary in different case combinations. For example, stripos("hello world", "HELLO") will return 0 because it is case-insensitive.

  • Parameters in URL : If you want to process a URL containing query parameters, stripos can also help us check whether the URL starts with a specific path or parameter. Note that the part of the parameter (usually starting with ? ) should not be confused with the opening part.

  • Performance considerations : Although stripos is efficient enough in most cases, if you process very large strings, or if you need to perform frequent string matching operations, it is recommended to test performance to determine whether further optimization is required.

4. Conclusion

stripos is a very convenient function that can help us easily determine whether a string contains another string in PHP, especially when we want to check the beginning of a string. With simple code implementation, we are able to verify whether a string starts with a specific vocabulary, whether in a regular string or a URL.