Current Location: Home> Latest Articles> How to calculate the position difference of substring using strrpos and strlen

How to calculate the position difference of substring using strrpos and strlen

gitbox 2025-06-03

In PHP, string manipulation is a very important part of daily development. The two functions strrpos and strlen are often used to process the calculations related to the position and length of substrings in strings. This article will introduce in detail how to use these two functions to calculate the position difference of substrings and analyze them based on actual application scenarios.


1. Introduction to strrpos and strlen

  • strrpos(string $haystack, string $needle, int $offset = 0): int|false
    This function is used to find the last time $needle appears in the string $haystack , and returns the index of the position (starting from 0). If not found, false is returned.

  • strlen(string $string): int
    Returns the length of the string $string .

These two functions can be used in combination to easily calculate the positional relationship of certain substrings in a string.


2. How to calculate the position difference of substrings

Suppose we have a string $text , and we want to calculate the gap between the last occurrence of a substring $sub from the end of the string.

Code Example

 <?php
$text = "https://gitbox.net/path/to/resource.html";
$sub = "/";

// Get the last occurrence of the substring
$pos = strrpos($text, $sub);

if ($pos !== false) {
    // Get the total length of the string
    $length = strlen($text);

    // Calculate the distance from the last occurrence position of the substring to the end of the string
    $distance = $length - $pos - 1;

    echo "Substring '{$sub}' Last appeared in location {$pos}。\n";
    echo "There is still a distance from the end of the string {$distance} Characters。\n";
} else {
    echo "Substring '{$sub}' not found。\n";
}
?>

Code parsing:

  1. Use strrpos to get the last substring / position, such as the last slash in the path.

  2. Use strlen to get the entire string length.

  3. The number of characters from the end of the string is obtained by subtracting the position and 1 of the substring (because the position starts from 0).

  4. The result is the difference between the substring and the end of the string.


3. Common application scenarios

1. Get the file name

When we process URLs or paths, we often need to intercept the file name after the last slash.

 <?php
$url = "https://gitbox.net/path/to/resource.html";
$lastSlashPos = strrpos($url, "/");
$filename = substr($url, $lastSlashPos + 1);
echo "The file name is:{$filename}";
?>

2. Dynamic splicing path

Determine whether the path end contains a specific symbol and adjust the splicing method according to the differences.

 <?php
$path = "https://gitbox.net/path/to/";
$lastChar = substr($path, -1);

if ($lastChar !== "/") {
    $path .= "/";
}

$fullPath = $path . "resource.html";
echo $fullPath;
?>

4. Summary

The combination of strrpos and strlen provides us with powerful and flexible string processing capabilities, especially in terms of path parsing, URL processing, etc., which can easily calculate the position difference of substrings, thereby achieving more complex string operation requirements.

Mastering the usage of these two functions will greatly improve your PHP string processing efficiency and code quality.