Page 1 of 1

taking the first few characters of a string

Posted: Wed Jan 10, 2007 7:37 am
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.

Posted: Wed Jan 10, 2007 7:45 am
by Grim...
You need substr().

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

Code: Select all

$small_surname = $surname{4};

Posted: Wed Jan 10, 2007 7:58 am
by dhrosti
substr() works perfect, Thanks.