Page 1 of 1
[SOLVED] print first letter of string
Posted: Fri Jan 27, 2006 8:57 am
by $var
how do you trim down the output of a database entry,
like a first name so that instead of
Chris Brown
you'd get either
C. Brown
- or -
C Brown
?
Posted: Fri Jan 27, 2006 8:59 am
by hawleyjr
First you would have to split up the string (By spaces I believe)
Then, get the first char of the first word:
$var = 'Chris';
$first_letter = $var{0}
Next put the string back together w/o the first word

Posted: Fri Jan 27, 2006 9:09 am
by Technocrat
Just as an FYI you can also use
substr but hawleyjr's code is faster
Posted: Fri Jan 27, 2006 9:50 am
by $var
thanks guys...
both are helpful
i have a question though...
$var{0} : what is this doing?
{0} what is the significance of the 0?
Posted: Fri Jan 27, 2006 9:58 am
by feyd
byte offset zero.
Posted: Fri Jan 27, 2006 10:10 am
by $var
i don't suppose there is a stupificated explantion of that... like...
the zero is for this reason, and it is relevant because it can be used in these other ways?
Posted: Fri Jan 27, 2006 10:14 am
by feyd
indexes into strings are zero based, like standard arrays. The zeroth byte is the first byte in the string, followed by the oneth being the second and so forth.
Posted: Fri Jan 27, 2006 10:15 am
by $var
thanks feyd. you rock.
Posted: Fri Jan 27, 2006 11:08 am
by hawleyjr
$var wrote:thanks feyd. you rock.
What am I chopped liver?
j/k
