Page 1 of 1
Printing 5 chars from a variable
Posted: Thu Sep 19, 2002 5:12 pm
by DynamiteHost
I did know how to do this... but i've forgotten
I want to print the first five characters from $var, anyone know how?
Thanks

try
Posted: Thu Sep 19, 2002 5:26 pm
by AVATAr
try substr() in the php manual...
ex:
Code: Select all
$rest = substr("abcdef", 1); // returns "bcdef"
$rest = substr("abcdef", 1, 3); // returns "bcd"
$rest = substr("abcdef", 0, 4); // returns "abcd"
$rest = substr("abcdef", 0, 8); // returns "abcdef"
Posted: Fri Sep 20, 2002 6:59 am
by Takuma
So to do that with AVATAr saids
Code: Select all
<?php
echo substr($variable,0,4);
?>
Posted: Fri Sep 20, 2002 7:07 am
by twigletmac
Takuma - that would only return the first
4 characters from the string not the first
5 which was what DynamiteHost was trying to do. This is clearly illustrated by one of the examples in the post above yours:
Code: Select all
$rest = substr("abcdef", 0, 4); // returns "abcd"
He also probably could've easily worked out the answer from the information in the manual and the manual examples that AVATAr posted. So not only was your post wrong it was also unneccessary.
I appreciate your wish to help others but posting without thinking or testing is more likely to confuse than to help.
Mac