Web Hosting Talk







View Full Version : Quick PHP Question


Smirks
04-08-2002, 03:11 PM
Hello,

I have a quick question regarding php. Let's say I have a string of numbers: 1234567890, and out of that string I only want to print the first 2 and the last 2. How can I do that in PHP?

Thanks,
S

AudiBoy
04-08-2002, 03:20 PM
Something like

$num = 123445678;
$length = strlen ($num);

echo substr($num, 0, 1); // prints the first two

echo substr($num,$length-1,$length); // prints the last two

AudiBoy
04-08-2002, 03:27 PM
turns out you don't even need to do all that...

echo substr($number, -2); // returns the last two


learn something every day

jay.

(SH)Saeed
04-08-2002, 03:29 PM
Actually, the above example prints the first and the last number. Change the last two lines to the following..

echo substr($num, 0, 2); // prints the first two
echo substr($num,$length-2,$length); // prints the last two

Smirks
04-08-2002, 03:35 PM
Thanks! Works like a charm!

Originally posted by AudiBoy
turns out you don't even need to do all that...

echo substr($number, -2); // returns the last two


learn something every day

jay.