I've always read that PHP's string functions were faster than using regex, so I've stuck with substr. I've used sprintf to format the string simply as it's more compact - not sure if it's more or less understandable at a glance though. Also, I'm not explicitly casting $xx to an int, as adding 1 should cause PHP to do that, and I've specified the parameter as a decimal value to sprintf.

Off Topic:

Pullo, just out of interest, how would you do this in Ruby?

Pullo
—
2013-08-22T11:23:08Z —
#9

fretburner said:

I've always read that PHP's string functions were faster than using regex, so I've stuck with substr. I've used sprintf to format the string simply as it's more compact - not sure if it's more or less understandable at a glance though. Also, I'm not explicitly casting $xx to an int, as adding 1 should cause PHP to do that, and I've specified the parameter as a decimal value to sprintf.

That makes sense. I didn't think of using sprintf.Nice one.

fretburner said:

Off Topic:

Pullo, just out of interest, how would you do this in Ruby?

Making the assumption that it always starts with 'xs-', you could do this:

This is taking advantage of the fact that the Ruby String class has several methods that let you produce successive strings — that is, strings that increment, starting at the rightmost character (next being one of them).

system
—
2013-08-22T16:08:27Z —
#10

How about javascript?A generic function, for any given separator and for any given length for the zero padded number, that also accounts for number going over their initial unit.

One other thing I would point out, is that in my javascript generic function I also handle the prefix (the part before the separator) without it being hardcoded, i.e. no serial.replace(/^xs\- or return xs-.

Drummin
—
2013-08-22T20:04:23Z —
#16

One other thing I would point out, is that in my javascript generic function I also handle the prefix (the part before the separator) without it being hardcoded, i.e. no serial.replace(/^xs\- or return xs-. And so does my solution with(out) resorting to JS.

Pullo
—
2013-08-22T20:17:16Z —
#17

myty said:

One other thing I would point out, is that in my javascript generic function I also handle the prefix (the part before the separator) without it being hardcoded

Yup, fair point. I was only messing about and seeing what was possible.

Drummin said:

And so does my solution with resorting to JS.

Shouldn't that be "without"

Can anyone add any more languages?I know this is the PHP forum, but it would be fun to see what's possible.

fretburner
—
2013-08-22T20:57:36Z —
#18

Pullo said:

Can anyone add any more languages?I know this is the PHP forum, but it would be fun to see what's possible.