Current Location: Home> Latest Articles> Why does stripos return false but not the result I thought?

Why does stripos return false but not the result I thought?

M66 2025-06-03

In PHP, the stripos function is used to find the location where a string first appears in another string. Unlike strpos , strpos is case-insensitive. So, when we use stripos in our code, if there is something we are looking for in the string, but it returns false , why is this? Today we will make an in-depth analysis.

1. Basic usage of stripos function

The basic syntax of the stripos function is as follows:

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

  • $needle : The substring to be found.

  • $offset (optional): Specifies the starting location of the search.

This function returns the location where the substring $needle first appears in $haystack . If it is found, it returns the index value of that location; if it is not found, it returns false .

2. Why does stripos return false?

(1) Needle is not in haystack

This may seem simple, but sometimes we ignore details, such as:

  • Check for extra spaces.

  • Whether a character set that does not match is used.

  • Whether the string encoding is consistent.

For example, suppose we are looking for the substring "exAMPle" in the string "exAMPle" . Although the content of the two strings is the same, if the encoding is inconsistent or contains extra spaces, stripos may return false .

 $haystack = "This is an example string";
$needle = "exAMPle";

$result = stripos($haystack, $needle);

if ($result === false) {
    echo "No string found!";
} else {
    echo "Find the string,The location is: " . $result;
}

Here, although "exAMPle" actually exists in "This is an example string" , stripos may still return false if the needle does not exactly match the case or space of the haystack .

(2) URL issues

Sometimes, if the URL is included in the search string, the reason why stripos returns false may be due to the formatting of the URL. For example, if the domain name in the URL does not match exactly, or there are extra characters before and after, it may also cause the match to fail.

Suppose we have the following code:

 $haystack = "Visit us at https://www.m66.net/welcome";
$needle = "m66.net";

$result = stripos($haystack, $needle);

if ($result === false) {
    echo "No URL was found!";
} else {
    echo "Find the URL,The location is: " . $result;
}

In this example, we want to find the string m66.net . If $haystack and $needle exactly match and there are no other interfering characters, stripos should be able to return the index value found. However, if the URL is incorrectly spliced ​​or contains additional characters (such as extra slashes or spaces), this may cause the lookup to fail.

3. Solution

(1) Check whether the string matches exactly

Make sure the string you are looking for is free of unexpected characters or spaces. You can use the trim() function to remove whitespace characters at both ends of the string, or use strtolower() to uniformly case.

 $haystack = "Visit us at https://www.m66.net/welcome";
$needle = "m66.net";

$haystack = trim($haystack);
$needle = strtolower($needle);

$result = stripos($haystack, $needle);

(2) Normalization of URL format

If your code involves matching URLs, make sure your URL format is uniform. If you only care about the domain name part, you can extract the domain name part of the URL before matching.

 $haystack = "Visit us at https://www.m66.net/welcome";
$needle = "m66.net";

// use parse_url Extract the domain name
$parsed_url = parse_url($haystack, PHP_URL_HOST);

$result = stripos($parsed_url, $needle);

if ($result !== false) {
    echo "Find the domain name!";
} else {
    echo "未Find the domain name!";
}

4. Summary

The reason why stripos returns false is usually related to the format of the target string and substring. Whether it is because the string does not match exactly, or because of the URL format or other factors, understanding these details and performing appropriate string preprocessing can help us avoid this problem.

Hope this article will be helpful for you to understand the use of stripos functions! If you encounter similar string matching problems, you might as well check the format and content of the strings to ensure they are correct.