I have a couple things to add. For increased efficiency, you should move the strlen() call to outside of the loop. The way you've written it, strlen() gets called each iteration of the loop, even though you know you're not modifying the length of the string.
Also, I don't understand what it is your loop is actually doing. What does the loop iteration variable, $x, have anything to do with finding matches in the original string?
It seems that this function can return any number of possibilities:
1) the longest substring of incrementing numbers,
2) the first substring of incrementing numbers,
3) a structure or array containing ALL substrings of incrementing numbers.
In each case, you can save yourself the overhead of copying the substring into new strings by returning the start position and length of the substring.
For example:
// This function finds the first substring, if any, where at least
// two characters in succession are incrementing digits.
function find_incrementing_substring(&$str) // pass by reference
{
$len = strlen($str)
if ($len < 2)
{
return false; // can't match empty or 1-char strings
}
$last = (int)$str[0];
$substr_start = 0;
$substr_length = 1;
for ($i = 1; $i < $len; ++$i) // start loop on 2nd char in str
{
if ((int)$str[$i] == ($last + 1))
{
++$substr_length;
}
else if ($substr_length > 1) // already matched SOMEthing
{
break;
}
else // no match yet, move start position to current char.
{
$substr_start = $i;
}
$last = (int)$str[$i];
}
return $substr_length? array($substr_start, $substr_length) : false;
}
Do you follow the logic? Basically, we keep tabs of if, where a sequence starts, and how long it is.
If we STOP matching an existing sequence, we break from the loop and return the start/length of the found sequence.
Otherwise, we assume we haven't started matching anything yet, and advance our start pointer.
It would be very easy to modify this function to return an array of ALL such embedded sequential substrings instead of just the first one.
Take care,
Nik
http://www.bigaction.org/