Page 1 of 1
First word in string
Posted: Sun Jun 23, 2002 12:58 pm
by larrytech
Hi,
If i have a variable like
$name = "Joe Blogs";
how can I trim it so it just says "Joe". I need these to work for all length not just 3 characters!
Thanks,
Larru
Posted: Sun Jun 23, 2002 2:00 pm
by will
here ya go....
Code: Select all
$name = ereg_replace("\ .*$","",$name);
this will also work if they have more than two names (say a middle name as well)... it will trim it down up to the first space.
Posted: Sun Jun 23, 2002 2:53 pm
by larrytech
Thanks alot! That's great.
Posted: Sun Jun 23, 2002 3:27 pm
by will
just thought about it.... you might use this instead, so that you could retrieve their last name also if you so choose later...
Code: Select all
$name = 'John Doe';
$nameArray = explode(" ",$name);
//first name
echo $nameArrayї0];
//second name
echo $nameArrayї1];
//etc, etc
only problem you might run into is if their last name is something like St. James.... it would break that apart. for this, you would need to know if they were specifying a middle name or not.
Posted: Mon Jun 24, 2002 1:01 pm
by larrytech
Great! That is even more useful. Luckily I can't think of many folks with surnames like that

!