David
David

Reputation: 2423

occurrence of string

i know you can find the first and last occurrence in a string using strstr() and strchr but how do i find the second occurrence, and the third occurrence of needle inside haystack? im using this to find the last occurrence of needle and the first occurrence of another needle and their position, then return the string that is in between each. thank you.

Upvotes: 2

Views: 413

Answers (3)

Matthew Flaschen
Matthew Flaschen

Reputation: 284796

For the last occurrence, use strrpos.

$lastPos = strrpos($haystack, $needle);

It takes an optional offset argument too.

Upvotes: 0

Svisstack
Svisstack

Reputation: 16616

You must find by strstr(), get result and start finding from result for time when strstr dont find nothing new.

You can use for it: http://php.net/manual/en/function.strpos.php

Upvotes: 1

Mitch Dempsey
Mitch Dempsey

Reputation: 39879

You can use strpos() and update the offset value to be after whatever you just matched.

Upvotes: 2

Related Questions