Larry Ullman's Book Forums

# General - Identifying The Digits In A 'Numerical' String

## Recommended Posts

Hi Everyone;

In my students database, each has a unique student_no made up of 7 (number) digits e.g.

1230045

where the digits signify:

i) 12 (First TWO digits) - Year of enrollment

ii) 3 (THIRD digit) - Department as well as level (Masters or PhD)

iii) 0 (4th digit) - Type of student (Short/Long term)

iv) 045 (Last 3 Digits) - the quarter of enrollment: April, July, October or January

Q: In any given student no, what PHP function can I use to identify (isolate) the above sets of digits, which I can then use to identify the student completely? i.e. From the set how can I isolate the numbers;

12,

3,

0,

045

Which I can then use to perform complete identification of the student(s)?

##### Share on other sites

Probably the easiest thing to do would be some simple math.

For example, to get the first two digits, divide the number by 100,000 and then truncate the resulting decimal to an integer.

To show what I mean:

1230045 / 100000 = 12.30045

(int) 12.30045 = 12

You could also use the floor function, but it accomplishes the same thing as type casting the float as an integer.

From there, you could then subtract 12 * 100000 from the original number, giving you 30045, and go from there.

That's the solution I would go with.

Another possibility though would be to convert the number to a string and then use the substr function.