Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Lookup repeating values or strings
<?php
/**
* Look for consecutive repeating elements
*
* @param $subject string|array|integer|Iterator
*
* @return boolean
*
* @author Mike Pretzlaw
* @link http://www.hallo-php-welt.de
* @version 1.0.1
*
*/
function hasOrderedRepeats($subject, $times = 2)
{
$prev = null;
if (is_scalar($subject)) $subject = str_split($subject);
foreach ((array) $subject as $element) {
$count = ($element == $prev)? $count + 1: 1;
if ($count >= $times) return true;
$prev = $element;
}
return false;
}
//var_dump(hasOrderedRepeats($argv[1], $argv[2]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment