Printing 5 chars from a variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DynamiteHost
Forum Commoner
Posts: 69
Joined: Sat Aug 10, 2002 5:33 pm

Printing 5 chars from a variable

Post 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 :D
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

try

Post 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"
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

So to do that with AVATAr saids

Code: Select all

<?php
echo substr($variable,0,4);
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply