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

后端 未结 14 1434
广开言路
广开言路 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条回答
  • 2020-11-27 05:33

    I know this question is kind of old, but here's a function I wrote to get the Xth occurrence of a substring, which may be helpful for other people that have this issue and stumble over this thread.

    /**
     * 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);
        } elseif ($number > 1) {
            return strpos($haystack, $needle, strposX($haystack, $needle, $number - 1) + strlen($needle));
        } else {
            return error_log('Error: Value for parameter $number is out of range');
        }
    }
    

    Or a simplified version:

    function strposX($haystack, $needle, $number = 0)
    {
        return strpos($haystack, $needle,
            $number > 1 ?
            strposX($haystack, $needle, $number - 1) + strlen($needle) : 0
        );
    }
    
    0 讨论(0)
  • 2020-11-27 05:33

    http://php.net/strpos

    $newstring = 'abcdef abcdef';
    $pos = strpos($newstring, 'a', 1);  // $pos = 7, not 0
    
    0 讨论(0)
  • 2020-11-27 05:35

    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));
    
    0 讨论(0)
  • 2020-11-27 05:38

    To find second occurrence of the string you can use the strpos along with "offset" parameter, by adding previous offset to strpos.

    $source = "Hello world, how you doing world...world ?";
    $find = "world";
    $offset = 0;
    $findLength = strlen($find);
    $occurrence = 0;
    
    while (($offset = strpos($source, $find, $offset))!== false) {
        if ($occurrence ++) {
          break;
        }
        $offset += $findLength; 
    }
    
    echo $offset;
    

    You can find all the occurrences by storing offset into an array

    while (($offset = strpos($source, $find, $offset))!== false) {
      $occurrences[] = $offset;
      $offset += $findLength; 
    }
    var_export($occurrences);
    

    Or can get a specific occurrence by matching $occurrence

    //find 3rd occurrence
    if ($occurrence ++ == 2) {
        break;
    }
    
    0 讨论(0)
  • 2020-11-27 05:38
    function substr_Index( $str, $nth ){
        $str2 = '';
        $posTotal = 0;
        for($i=0; $i < $nth; $i++){
    
            if($str2 != ''){
                $str = $str2;
            }
    
            $pos   = strpos($str, ':');
            $str2  = substr($str, $pos+1);
            $posTotal += $pos+1;
    
        }
        return $posTotal-1;
    }
    
    
    echo substr($mystring, substr_Index( $mystring , 2) );
    

    Function returns position of nth delimiter.

    Second parameter of substr_Index must be bigger than 0;

    To find second occourance use substr_Index( $mystring , 2)

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题