Avatar of rgb192
rgb192
Flag for United States of America asked on

parse ' ' and ',' to make names

parse firstname lastname by ' '(space)
 
$name="Peter L. Panda";

$temp=explode(" ",$name,2);
$firstname=$temp[0]; //==Peter
$lastname=$temp[1]; //==L. Panda


but if name contains a ',' (comma)

last_name,first_name  where last_name is the first word

smith, john
first=john
last=smith

jones, john L
first=john L
last=jones

evans, mary lee
first= mary lee
last=evans
PHPRegular Expressions

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Ray Paseur

Interesting problem.  In the first example, the $lastname will not be L. Panda.

I'll see if I can come up with an example that will be useful.  Are you certain that you do not want to account for middle names?
ASKER CERTIFIED SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Ray Paseur

Apologies -- I spoke before testing.  In the first example, the value of $temp[1] will, in fact, be L. Panda.  I overlooked the limit integer in the call to the explode() function.
rgb192

ASKER
works for , and ' ' thanks
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Ray Paseur

Thanks for the points! ~Ray