taking the first few characters of a string

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
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

taking the first few characters of a string

Post by dhrosti »

Is it possible to take the first 4 characters of a surname, for example, and store them in a variable?

Im sure there's probably a built-in function that performs this task but im yet to find it and i hope someone here can help.

I guess it might look something like...

Code: Select all

$surname = $_POST['surname'];
$short_surname = some_function($surname, '4');
But maybe not.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

You need substr().

Or, in that situation, I think you can just use

Code: Select all

$small_surname = $surname{4};
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post by dhrosti »

substr() works perfect, Thanks.
Post Reply