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

后端 未结 14 1429
广开言路
广开言路 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:17

    Old question but how about using explode?

    $corpus = "how many words are there in a dictionary? I'm not going to count them word by word...";
    $looking_for = 'word';
    $instance = 2;
    
    $parts = explode($looking_for, $corpus, $instance + 1);
    array_pop($parts);
    $position = strlen(implode($looking_for, $parts));
    
    0 讨论(0)
  • 2020-11-27 05:20

    The recursive function from Smokey_Bud was slowing my script drastically down. Using a regular expression is much faster in this case (for finding any occurence):

    function strposX($haystack, $needle, $number)
    {
        // decode utf8 because of this behaviour: https://bugs.php.net/bug.php?id=37391
        preg_match_all("/$needle/", utf8_decode($haystack), $matches, PREG_OFFSET_CAPTURE);
        return $matches[0][$number-1][1];
    }
    
    // get position of second 'wide'
    $pos = strposX('Hello wide wide world', 'wide', 2);
    
    0 讨论(0)
  • 2020-11-27 05:21

    Please check the following code ... it works pretty fine for me.

    <?php
        function f_srch ($s1, $par) {
            echo 'Searching for [' . $par . '] in [' . $s1 . ']<br>';
            $k = 0; //while loop
            $i = 0; // counter
    
            while ($k >= 0) {
                $pos = strpos($s1, $par, $k);
                if ($pos === false) {
                    $k=-1;
                    echo 'Letter not found'.'<br>';
                } else {
                    if ($pos == $k) { 
                        echo 'The position of the letter is '.$pos.'<br>'; 
                        $i++;
                    }
                    $k++;
                } 
            }
            echo 'The letter was found ' . $i . ' time(s).'.'<br>'; 
        }
        f_srch('i am searching for a letter in this sentence','t');
    ?>
    
    0 讨论(0)
  • 2020-11-27 05:22

    Simple is beautiful

    function strposX($haystack, $needle, $n = 0)
    {
        $offset = 0;
    
        for ($i = 0; $i < $n; $i++) {
            $pos = strpos($haystack, $needle, $offset);
    
            if ($pos !== false) {
                $offset = $pos + strlen($needle);
            } else {
                return false;
            }
        }
    
        return $offset;
    }
    
    $offset = strposX($result, "\n", $n);
    
    if ($offset === false) {
        $offset = strlen($result) - 1;
    }
    
    0 讨论(0)
  • 2020-11-27 05:29

    just worked for me to find if are 2 or more occurrence of a char, then by strlen them i found that exist 2 occurrence ex ( i dont use $matches at all):

    $string = '1234|6|#red';
    
    if(strlen(preg_match_all('/|/', $string,$matches, PREG_OFFSET_CAPTURE)) ==2){
    
    echo 'i have 2 occurence of char: |';
    
        }
    
    0 讨论(0)
  • 2020-11-27 05:31

    Old question, but if someone's looking for a way to find occurrences from the END of the string (for example 3rd occurrence of dot from the end) the following function works (didn't want to use oncodes function not to mess with encoding)

    $str = "NooooYesYesNo";
    
    function find_occurence_from_end($haystack, $needle, $num) {
    
        for ($i=1; $i <=$num ; $i++) {
    
            # first loop return position of needle
            if($i == 1) {
                $pos = strrpos($haystack, $needle);
            }
    
            # subsequent loops trim haystack to pos and return needle's new position
            if($i != 1) {
    
                $haystack = substr($haystack, 0, $pos);
                $pos = strrpos($haystack, $needle);
    
            }
    
        }
    
        return $pos;
    
    }
    
    $pos = find_occurence_from_end($str, "Yes", 2);
    
    // 5
    

    It's super simple. Basically each time it finds an occurrence of your needle it "trims" the string to that position. So it keeps on trimming it while returning the latest position each time.

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