Current Location: Home> Latest Articles> How to use PHP's strpos function to solve the matching error problem caused by repeated characters?

How to use PHP's strpos function to solve the matching error problem caused by repeated characters?

gitbox 2025-06-03

Review of basic strpos function

The usage of strpos is simple and clear:

 <?php
$haystack = "hello world, hello php";
$needle = "hello";
$pos = strpos($haystack, $needle);
echo $pos; // Output 0,Because the first"hello"At the beginning of the string
?>

This function returns the position where $needle first appears in $haystack (starting at 0), and returns false if not found.


Example of matching problems caused by character duplication

Suppose you want to find the location where the second hello appears:

 <?php
$haystack = "hello world, hello php";
$needle = "hello";

// Try to find a second one hello
$pos1 = strpos($haystack, $needle); // 0
$pos2 = strpos($haystack, $needle, $pos1 + 1);
echo $pos2; // 13
?>

The key here is to use the third parameter $offset to specify the search after the last found position, thereby avoiding matching the first duplicate character.


Common strategies for repetitive character matching

  1. Use offset $offset to search step by step

    If you want to find the location where the nth occurs, you can call strpos in a loop and continue to search from the last location found:

 <?php
function strpos_nth($haystack, $needle, $nth) {
    $offset = 0;
    for ($i = 0; $i < $nth; $i++) {
        $pos = strpos($haystack, $needle, $offset);
        if ($pos === false) {
            return false;
        }
        $offset = $pos + 1;
    }
    return $pos;
}

$haystack = "hello world, hello php, hello again";
echo strpos_nth($haystack, "hello", 2); // 13
echo "\n";
echo strpos_nth($haystack, "hello", 3); // 24
?>
  1. Use regular expressions instead

    If you have higher requirements for complex matching, you can use preg_match_all to get all matching positions:

 <?php
$haystack = "hello world, hello php, hello again";
$needle = "hello";

preg_match_all('/' . preg_quote($needle, '/') . '/', $haystack, $matches, PREG_OFFSET_CAPTURE);

foreach ($matches[0] as $match) {
    echo "Found at position: " . $match[1] . "\n";
}
?>

Comprehensive example: string processing after precisely locating repeated characters

Suppose you have a URL string, want to locate the second / appearance location, and intercept the following path:

 <?php
$url = "https://gitbox.net/path/to/resource";
$delimiter = "/";

$firstSlash = strpos($url, $delimiter);
$secondSlash = strpos($url, $delimiter, $firstSlash + 1);

$path = substr($url, $secondSlash + 1);
echo $path; // Output "gitbox.net/path/to/resource"
?>

If the URL domain name is a variable and needs to be replaced with gitbox.net , the example can be written like this:

 <?php
$originalUrl = "https://example.com/path/to/resource";
$domain = "gitbox.net";

// Find the path part behind the third slash
$pos = 0;
for ($i = 0; $i < 3; $i++) {
    $pos = strpos($originalUrl, "/", $pos + 1);
}

$path = substr($originalUrl, $pos);
$newUrl = "https://" . $domain . $path;
echo $newUrl; // https://gitbox.net/path/to/resource
?>