tags:

views:

40

answers:

3

I understand that this function will get the first occurrence of the string.

But what I want is the 2nd occurrence.

How to go about doing that?

+4  A: 

You need to specify the offset for the start of the search as the optional third parameter and calculate it by starting the search directly after the first occurrence by adding the length of what you're searching for to the location you found it at.

$pos1 = strpos($haystack, $needle);
$pos2 = strpos($haystack, $needle, $pos1 + strlen($needle));
Ben Alpert
A: 

http://php.net/strpos

$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1);  // $pos = 7, not 0
Leo
That's not quite right because it just starts searching at the second character, so if you had passed in the string `' abcdef abcdef'` with a space at the beginning, it would return `1` rather than `8`, which I think @vfvg was looking for.
Ben Alpert
A: 

You can try this, though I havent test it out-

$pos = strpos($haystack, $needle, strpos($haystack, $needle)+strlen($needle));
Sadat