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
$var
Forum Contributor
Posts: 317 Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto
Post
by $var » Fri Jan 27, 2006 8:57 am
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
?
Last edited by
$var on Fri Jan 27, 2006 9:36 am, edited 1 time in total.
hawleyjr
BeerMod
Posts: 2170 Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA
Post
by hawleyjr » Fri Jan 27, 2006 8:59 am
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
Technocrat
Forum Contributor
Posts: 127 Joined: Thu Oct 20, 2005 7:01 pm
Post
by Technocrat » Fri Jan 27, 2006 9:09 am
Just as an FYI you can also use
substr but hawleyjr's code is faster
$var
Forum Contributor
Posts: 317 Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto
Post
by $var » Fri Jan 27, 2006 9:50 am
thanks guys...
both are helpful
i have a question though...
$var{0} : what is this doing?
{0} what is the significance of the 0?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Jan 27, 2006 9:58 am
byte offset zero.
$var
Forum Contributor
Posts: 317 Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto
Post
by $var » Fri Jan 27, 2006 10:10 am
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?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Jan 27, 2006 10:14 am
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.
$var
Forum Contributor
Posts: 317 Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto
Post
by $var » Fri Jan 27, 2006 10:15 am
thanks feyd. you rock.
hawleyjr
BeerMod
Posts: 2170 Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA
Post
by hawleyjr » Fri Jan 27, 2006 11:08 am
$var wrote: thanks feyd. you rock.
What am I chopped liver?
j/k