Um, I'm kinda stepping in a bit late here, but...
You want to find strings of the form Nn.a, where n is digits, a is letters, and N is $auto_id. But you don't want to match MNn.a just because the sequence of digits MN happens to end in N; so if $auto_id is 5, you want to match 5_12.jpg, 5_88.gif, but not 25_91.jpg.
It's going to be a real pain doing this with ereg, because the regexp syntax it uses is not sophisticated enough. The PCRE syntax of preg_match() on the other hand is sophisticated enough to make it fairly simple. (preg_match() is also faster than ereg()...)
So, digits ($auto_id, in fact), followed by _ followed by digits, followed by . followed by letters.
if(preg_match("/(?<!\d){$auto_id}_\d+\.[a-z]+/i", $string, $matches))
{ ... the first match is in $matches[0]...
}
Dissecting that regexp:
/
Start of the regexp.
color=darkblue[/color]
Something called a "negative lookbehind assertion". Fancy way of saying that "the matched string must not be preceded by anything matching this", where in this case "this" is \d - PCRE for a digit. The upshot of this is that if you're looking for "5", "25" won't match because the "5" is preceded by a digit.
{$auto_id}
$auto_id itself - wrapped in {} to stop PHP getting confused about where the variable name ends.
_
An underscore.
\d+
More digits
.
A .
[a-z]+
One or more letters
/
End of the regexp
i
Case-insensitive matching. I could have left this off and used [a-zA-Z] instead, I suppose...
Note also that there is preg_match_all(), which will return all matches of the pattern with one call.