PHP StrPos() Find Second Occurrence of a String

Posted on September 3, 2017 at 12:34 pm

Simple PHP code to find the second occurrence of a string with strpos():

$pos1 = strpos($haystack, $needle);
$pos2 = strpos($haystack, $needle, $pos1 + strlen($needle));

You can make it a one-line like this:

$pos2 = strpos($haystack, $needle, strpos($haystack, $needle) + strlen($needle));

Other Posts

Updated Posts