Current Location: Home> Latest Articles> What is the difference between stripos, strpos and stritr? Detailed explanation of the usage scenarios and functions of the three

What is the difference between stripos, strpos and stritr? Detailed explanation of the usage scenarios and functions of the three

M66 2025-05-31

In PHP, string processing functions are very common, stripos , strpos and strristr are the three most commonly used functions, all of which are used to find the position of a substring in another string, but they differ in some details. This article will explain in detail the differences, usage scenarios and specific functions of these three functions.

1. Function Overview

  1. strpos :

    • Function : Find where a string (substring) first appears in another string.

    • Case sensitive : Yes, strpos is case sensitive.

    • Return value : Returns the location where the substring first appears (starting at 0), and if not found, returns false .

  2. stripos :

    • Function : Similar to strpos , both are to find the location where a string first appears in another string.

    • Case insensitive : Unlike strpos , strpos is case-insensitive.

    • Return value : Returns the location where the substring first appears (starting at 0), and if not found, returns false .

  3. strristr :

    • Function : Find where a string (substring) first appears in another string and returns everything from the matching position to the end of the string.

    • Case insensitive : strrist is also case-insensitive.

    • Return value : Returns the matching substring and its subsequent content. If not found, return false .

The specific differences between the two and three

function Case sensitivity Return value type illustrate
strpos yes Match position (int) Find and return the first match
stripos no Match position (int) Find and return the first match (case insensitive)
strrist no String from matching position to end Find and return content from matching position to end of string (case insensitive)

3. Specific usage scenarios and examples

1. Use scenarios of strpos

strpos is suitable for situations where exact matches are required, especially if you care about case. For example, when checking whether a URL contains a specific substring, you need to make sure that the case is exactly consistent.

Sample code :

 <?php
$url = "https://www.m66.net/products";
$position = strpos($url, "m66.net");

if ($position !== false) {
    echo "URL Included in m66.net,Location:{$position}";
} else {
    echo "URL Not found in m66.net";
}
?>

This code will return the URL containing m66.net, location: 8 , because m66.net starts with the 8th character.

2. Stripos usage scenarios

stripos are suitable for cases where you don't care about upper and lower case, especially if you want to look for a substring if you want to ignore case. For example, when you process user input, you may not be able to control the case of the input, so you can use stripos .

Sample code :

 <?php
$url = "https://www.M66.net/Products";
$position = stripos($url, "m66.net");

if ($position !== false) {
    echo "URL Included in m66.net,Location:{$position}";
} else {
    echo "URL Not found in m66.net";
}
?>

This code will return the URL containing m66.net, position: 8 , regardless of whether M in the original string is uppercase or lowercase.

3. Use scenarios of strrist

strrist is useful for finding a substring and getting everything behind it, especially if you don't care about the matching position, but care about the matching content. For example, when parsing a URL or processing a string, you may need to intercept the part that starts from a certain matching position to the end.

Sample code :

 <?php
$url = "https://www.m66.net/products?id=123";
$result = stristr($url, "m66.net");

if ($result !== false) {
    echo "Matched string:{$result}";
} else {
    echo "No matching content was found";
}
?>

This code returns the matching string: https://www.m66.net/products?id=123 , which contains everything from m66.net to the end of the string.

4. Summary

  • strpos : case sensitive, suitable for scenarios that require exact matches.

  • stripos : case-insensitive, suitable for scenarios where case is not concerned.

  • strristr : case-insensitive, suitable for scenarios where content from matching positions to end of strings needs to be intercepted.

Choosing the right function depends on your specific needs, and understanding their differences can help you write more efficient and clear code.