Find the second (or third, or fourth) occurence in a string

PHP includes some handy functions to find the first or last occurrence of a given string token in a string: <a href="http://www.php.net/strpos">strpos</a> and <a href="http://www.php.net/strrpos">strrpos</a>. However these functions are limited to just the first occurrence; what if I want to know the location of the second token’s position, or the third? These problems usually result in some serious coding acrobatics. Well no need for code-jitsu anymore. Based almost completely on a post I found at another blog — which is now down, how’s that for timing? — here are two functions which allow you to search for any occurrence of a specific token in a string…lang="php"> /**

  • Find position of Nth $occurrence of $needle in $haystack

  • Starts from the beginning of the string

**/

function strpos_offset($needle, $haystack, $occurrence) {

// explode the haystack

$arr = explode($needle, $haystack);

// check the needle is not out of bounds

switch( $occurrence ) {

case $occurrence == 0:

  return false;

case $occurrence > max(array_keys($arr)):

  return false;

default:

  return strlen(implode($needle, array_slice($arr, 0, $occurrence)));

}

} /**

  • Find position of Nth $occurrence of $needle in $haystack

  • Starts from the end of the string

**/

function strrpos_offset($needle, $haystack, $occurrence) {

// explode the haystack

$arr = array_reverse(explode($needle, $haystack));

// check the needle is not out of bounds

switch( $occurrence ) {

case $occurrence == 0:

  return false;

case $occurrence > max(array_keys($arr)):

  return false;

default:

  $inverted = strlen(implode($needle, array_slice($arr, 0, $occurrence)));

  $actual = (strlen($haystack) - 1) - $inverted;

  return $actual;

}

} // look for second occurrence of letter ‘a’ from the start of string

echo strpos_offset('a’, 'abracadabra’, 2);

// returns 3 // look for second occurrence of letter 'a’ from the end of string

echo strrpos_offset('a’, 'abracadabra’, 2);

// returns 7

In terms of use, we've essentially added an extra argument to `strpos` and `strrpos` that specifies which occurrence you're looking for. In other words, you can make both of these functions work like the PHP standards by setting the third `$occurrence` variable to 1.