在字符串中搜索模式

Pattern search within a string.

for eg.

$string = "111111110000";
FindOut($string);

Function should return 0

function FindOut($str){    
    $items =  str_split($str, 3);    
    print_r($items);
}

What you have here can conceptually be solved with a sliding window. For your example, you have a sliding window of size 3.

For each character in the string, you take the substring of the current character and the next two characters as the current pattern. You then slide the window up one position, and check if the remainder of the string has what the current pattern contains. If it does, you return the current index. If not, you repeat.

Example:

1010101101
|-|

So, pattern = 101. Now, we advance the sliding window by one character:

1010101101
 |-|

And see if the rest of the string has 101, checking every combination of 3 characters.

Conceptually, this should be all you need to solve this problem.

Edit: I really don't like when people just ask for code, but since this seemed to be an interesting problem, here is my implementation of the above algorithm, which allows for the window size to vary (instead of being fixed at 3, the function is only briefly tested and omits obvious error checking):

function findPattern( $str, $window_size = 3) {
    // Start the index at 0 (beginning of the string)
    $i = 0;

    // while( (the current pattern in the window) is not empty / false)
    while( ($current_pattern = substr( $str, $i, $window_size)) != false) {
        $possible_matches = array();

        // Get the combination of all possible matches from the remainder of the string
        for( $j = 0; $j < $window_size; $j++) {
            $possible_matches = array_merge( $possible_matches, str_split( substr( $str, $i + 1 + $j), $window_size));
        }

        // If the current pattern is in the possible matches, we found a duplicate, return the index of the first occurrence
        if( in_array( $current_pattern, $possible_matches)) {
            return $i;
        }

        // Otherwise, increment $i and grab a new window
        $i++;
    }
    // No duplicates were found, return -1
    return -1;
}

It should be noted that this certainly isn't the most efficient algorithm or implementation, but it should help clarify the problem and give a straightforward example on how to solve it.

Based on the str_split documentation, calling str_split on "1010101101" will result in:

Array(
  [0] => 101
  [1] => 010
  [2] => 110
  [3] => 1
}

None of these will match each other.

You need to look at each 3-long slice of the string (starting at index 0, then index 1, and so on).

I suggest looking at substr, which you can use like this:

substr($input_string, $index, $length)

And it will get you the section of $input_string starting at $index of length $length.

Looks like you more want to use a sub-string function to walk along and check every three characters and not just break it into 3

function fp($s, $len = 3){
  $max = strlen($s) - $len; //borrowed from lafor as it was a terrible oversight by me
  $parts = array();

  for($i=0; $i < $max; $i++){
    $three = substr($s, $i, $len);
    if(array_key_exists("$three",$parts)){
          return $parts["$three"];  
    //if we've already seen it before then this is the first duplicate, we can return it
    }
    else{
      $parts["$three"] = i; //save the index of the starting position.
    }
  }

  return false; //if we get this far then we didn't find any duplicate strings
}

quick and dirty implementation of such pattern search:

function findPattern($string){
    $matches = 0;
    $substrStart = 0;

    while($matches < 2 && $substrStart+ 3 < strlen($string) && $pattern = substr($string, $substrStart++, 3)){
        $matches = substr_count($string,$pattern);
    }

    if($matches < 2){
        return null;
    }
    return $substrStart-1;

If I understand you correctly, your problem comes down to finding out whether a substring of 3 characters occurs in a string twice without overlapping. This will get you the first occurence's position if it does:

function findPattern($string, $minlen=3) {
    $max = strlen($string)-$minlen;
    for($i=0;$i<=$max;$i++) {
        $pattern = substr($string,$i,$minlen);
        if(substr_count($string,$pattern)>1)
            return $i;
    }
    return false;
}

Or am I missing something here?