Question about strpos: how to get 2nd occurrence of a string?

后端 未结 14 1536
广开言路
广开言路 2020-11-27 04:38

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?

14条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 05:43

    Easily, just do it:

    $i = $pos = 0;    
    do {
            $pos = strpos( $string, $needle, $pos+1 );
    } while( $i++ < $nth);
    

    $nth for your situation is equal to 2.

提交回复
热议问题