/** * Find the position of the Xth occurrence of a substring in a string * @param $haystack * @param $needle * @param $number integer > 0 * @return int */ function strposX($haystack, $needle, $number) { if ($number == 1) { return strpos($haystack, $needle); } return strpos($haystack, $needle, strposX($haystack, $needle, $number - 1) + strlen($needle)); }
function strposX($haystack, $needle, $number) { if ($number == '1') { return strpos($haystack, $needle); } elseif ($number > '1') { return strpos($haystack, $needle, strposX($haystack, $needle, $number - 1) + strlen($needle)); } else { return false; } }